Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicitly declared special member functions

Tags:

c++

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?

like image 952
Whoami Avatar asked Dec 21 '22 18:12

Whoami


1 Answers

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.

like image 68
juanchopanza Avatar answered Dec 24 '22 03:12

juanchopanza