C++ General: What members of a class are implicitly defined?
Q: What members of a class are implicitly defined?
A: There are quite a few things that you get when you make a class (if they aren't explicitly defined). The important thing to note is that they are not actually created/written by the compiler unless it's sure that you need them. Let's go ahead with an example empty class named 'MyClass'. Below are those members alongwith the constructs that force them into existence:
Default (zero-argument) constructor
Whenever an object of the class is created unless there are any parameterized constructors defined.
Code:
MyClass ObjectOfMyClass; // Object creation
Copy constructor
Whenever copy construction is initiated.
Code:
MyClass ObjectOfMyClass;
MyClass AnotherObjectOfMyClass = ObjectOfMyClass; // Copy construction
Assignment operator
Whenever the assignment operator '=' is used with objects of the class.
Code:
MyClass ObjectOfMyClass;
MyClass AnotherObjectOfMyClass;
// some more code manipulating the objects
ObjectOfMyClass = AnotherObjectOfMyClass; // Assignment operator
Destructor
If there is an explicitly/implicitly defined constructor (nothing to do with the keyword 'explicit') and an object is created. That would mean if you went through some code as stated in the first point, a destructor would be generated and called whenever the object is destroyed.
A pair of 'address-of' operators
There are two versions: a 'const' one and a 'non-const' one. When the '&' ('address-of') operator is used on a const object, first version is generated and the later one associates itself with a non-const object with similar usage.
Bookmarks