Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I define the following class</p> <pre><code>class X {}; </code></pre> <p>The compiler will define the following methods:</p> <pre><code>X::X() {} // Default constructor. It takes zero arguments (hence default). X::~X() {} // Destructor X::X(X const&amp; rhs) {}; // Copy constructor X&amp; operator=(X const&amp; rhs) {return *this;} // Assignment operator. </code></pre> <p>Note:<br> The default constructor is not built if <strong>ANY</strong> constructor is defined.<br> The other methods are not built if the user defines an alternative.</p> <p>What is slightly more interesting is the default implementation when we have members and a base:</p> <pre><code>class Y: public X { int a; // POD data int* b; // POD (that also happens to be a pointer) Z z; // A class }; // Note: There are two variants of the default constructor. // Both are used depending on context when the compiler defined version // of the default constructor is used. // // One does `default initialization` // One does `zero initialization` // Objects are zero initialized when // They are 'static storage duration' // **OR** You use the braces when using the default constructor Y::Y() // Zero initializer : X() // Zero initializer , a(0) , b(0) , z() // Zero initializer of Z called. {} // Objects are default initialized when // They are 'automatic storage duration' // **AND** don't use the braces when using the default constructor Y::Y() :X // Not legal syntax trying to portray default initialization of X (base class) //,a // POD: uninitialized. //,b // POD: uninitialized. ,z // Not legal syntax trying to portray default initialization of z (member) {} // // Note: It is actually hard to correctly zero initialize a 'automatic storage duration' // variable (because of the parsing problems it tends to end up a a function // declaration). Thus in a function context member variables can have indeterminate // values because of default initialization. Thus it is always good practice to // to initialize all members of your class during construction (preferably in the // initialization list). // // Note: This was defined this way so that the C++ is backward compatible with C. // And obeys the rule of don't do more than you need too (because we want the C++ // code to be as fast and efficient as possible. Y::Y(Y const&amp; rhs) :X(rhs) // Copy construct the base ,a(rhs.a) // Copy construct each member using the copy constructor. ,b(rhs.b) // NOTE: The order is explicitly defined ,z(rhs.z) // as the order of declaration in the class. {} Y&amp; operator=(Y const&amp; rhs) { X::operator=(rhs); // Use base assignment operator a = rhs.a; // Use the assignment operator on each member. b = rhs.b; // NOTE: The order is explicitly defined z = rhs.z; // as the order of declaration in the class. return(*this); } Y::~Y() { Your Code first } // Not legal code. Trying to show what happens. : ~z() , ~b() // Does nothing for pointers. , ~a() // Does nothing for POD types , ~X() ; // Base class destructed last. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload