Reference: Effective C++ Third Edition 55 Specific Ways to Improve Your Programs and Designs
Terminology
Declaration: Tells compilers about the name and type of something, but not details.
1 | extern int x; // object declaration |
Definition: provides compilers with the details a declaration omits. It is where compilers set aside memory for the object.
1 | int x; // object definition |
Initialisation:
- process of giving an object its first value.
- For objects of user-defined types -> initialisation performed by constructors.
- Default Constructor: can be called without any arguments or has default values.
Explicit:
- it prevents from being used to perform implicit type conversions, but may be used for explicit type conversions.
- Explicit constructors are more preferable than non-explicits as it prevents compilers from performing unexpected type converstions.
- If you dont have good reason to do implicit type conversion -> declare it as explicit!
1 | class B{ |
Copy Constructor: copy the another object of the same type.
Copy Assignment Operator: to copy the value from one object to another of the same type.
1 | class A{ |
How to distinguish? If a new object is being defined like a2 and a3, it is copy constructor. If an existing object copies another like a1 = a2, it is copy assignment constructor.
Possible undefined behavrious:
1 | int *p = 0; // p is a nullptr |