In the book I’m reading at the moment (C++ Without Fear) it says that if you don’t declare a default constructor for a class, the compiler supplies one for you, which ‘zeroes out each data member’. I’ve experimented with this, and I’m not seeing any zeroing -out behaviour. I also can’t find anything that mentions this on Google. Is this just an error or a quirk of a specific compiler?
Share
If you do not define a constructor, the compiler will define a default constructor for you.
Construction
The implementation of this
default constructor is:
Note:
The POD data (int,float,pointer, etc.) do not have an explicit constructor but the default action is to do nothing (in the vane of C++ philosophy; we do not want to pay for something unless we explicitly ask for it).
Copy
If no destructor/copy Constructor/Copy Assignment operator is defined the compiler builds one of those for you (so a class always has a destructor/Copy Constructor/Assignment Operator (unless you cheat and explicitly declare one but don’t define it)).
The default implementation is:
Destructor:
Copy Constructor:
Copy Assignment Operator:
Note Copy Construction/Assignment operator of POD Data is just copying the data (Hence the shallow copy problem associated with RAW pointers).
Move
If no destructor/copy Constructor/Copy Assignment/Move Constructor/Move Assignment operator is defined the compiler builds the move operators for you one of those for you.
The default implementation is:
Implicitly-declared move constructor If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:
Move Constructor:
Move Assignment Operator: