3

There are four special functions fundamentally implicitly declared.

  1. Default constructor
  2. Default destructor
  3. Default assignment operator
  4. Default member-wise copy operator

Question:

If any one of them is defined by user, [eg: Destructor ] rest of the three functions will not get declared. Is that the correct? or it is applicable only to default constructor, and copy Constructor?

Linuxios
  • 31,993
  • 12
  • 82
  • 110
Whoami
  • 12,364
  • 16
  • 75
  • 133

4 Answers4

5

If you declare your own constructor, of any type (including copy constructor), then the default constructor is no longer implicitly declared. The rest are (unless you have declared one of them yourself).

However, if you find yourself declaring any one of 2, 3 or 4, then you most likely should declare the other two, even if the compiler implicitly declares them. This is known as the rule of three.

Edit in C++11 there are also implicitly declared move copy constructor and a move assignment operator, so the rule of three becomes the rule of five.

juanchopanza
  • 210,243
  • 27
  • 363
  • 452
  • 3
    Make that "rule of five" with the introduction of move semantics. – yuri kilochek Jul 26 '12 at 14:21
  • @ManofOneWay of course it is, thanks! I can't believe I got three upvotes with that mistake. – juanchopanza Jul 26 '12 at 14:28
  • "However, if you find yourself declaring any one of 2, 3 or 4, then you most likely should declare the other two". -> If i declare any one of 2 or 3 or 4, then rest of the two functions declared implicitly? – Whoami Jul 26 '12 at 14:42
  • @Whoami yes they get implicitly declared, but the rule of three says that you should probably declare and implement your own. – juanchopanza Jul 26 '12 at 14:52
  • @Whoami yes, if the user provides a copy constructor, then the default constructor is no longer implicitly declared. But the default constructor is no longer implicitly declared if the user declares **any** constructor, single or multi-argument. But the other constructors are still implicitly declared (although I am not 100% sure that applies to the C++11 move constructors). – juanchopanza Jul 26 '12 at 15:08
3

No, you do not have to explicitly declare the others just because you are declaring one. However, if you declare one of 2), 3), 4), you probably need to declare the others as well.

This is called the rule of three. I believe it's called the rule of five in C++11.

Community
  • 1
  • 1
Man of One Way
  • 3,824
  • 1
  • 23
  • 40
  • I think they've settled on ["rule of five"](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11). – Cody Gray Jul 26 '12 at 14:22
1

If you define your own Foo(), the default ~Foo() will still be declared, and vise versa. Same with the assignment operator and the copy constructor. Just remember, that if you define a constructor with arguments, you lose the default constructor. You have to do this to get one too:

class Foo
{
public:
  Foo(); //Declares a default constructor.
  Foo(int);
}
Linuxios
  • 31,993
  • 12
  • 82
  • 110
0

There is very informative and concise table at the end of this page by Howard Hinnant https://howardhinnant.github.io/classdecl.html

enter image description here

Hackless
  • 313
  • 4
  • 14