Constructors, '=' Assignment-Operators, and Destructors (advanced, part #2)


Proper use of Constructors, '=' Assignment-Operators, and Destructors, which are the special member procedures for constructing/initializing, assigning, and destroying objects (part #2).


1. Compiler interaction (with default-constructor, copy-constructor, and copy-assignment operator)
During assignments or copy-constructions, the compiler interacts with the different calls of copy-assignment operators, default-constructors and copy-constructors, in order to optimize the copy for resulting objects, making the best use of the member procedures provided by the user (maybe non-exhaustively).

Notes:
A Type has a default-constructor if one among the following propositions is verified:
- It has an explicit default-constructor
- One at least of its field has an initializer.
- One at least of its members is an object having a default-constructor itself.
- Its Base (if exists) has a default-constructor (the built-in 'Object' has a default-constructor).

Under certain conditions, the explicit copy-assignment operator may be called for a copy-construction (if there is not an explicit copy-constructor).
But inversely, the explicit copy-constructor will never be called for an assignment (if there is not an explicit copy-assignment operator) regardless of the conditions.




2. Rules of good manners (for constructors, copy-constructors, copy-assignment operators, and destructors)
Reminder of behaviors impacting constructors, copy-constructors, copy-assignment operators, and destructors:
- Defining an explicit default-constructor replaces the implicit default-constructor built by the compiler.
- Defining an explicit constructor other than the one default suppresses the implicit default-constructor built by the compiler. In this precise case, there is no default-constructor at all!
- The implicit copy-constructor (or copy-assignment operator, or destructor) built by the compiler can be replaced by an explicit copy-constructor (or copy-assignment operator, or destructor) defined by the user.
- But (as opposed to the default-constructor), there is always a copy-constructor (or a copy-assignment operator or a destructor), either an implicit built by the compiler or an explicit defined by the user.
- When there is object composition, the composed object Type must have an implicit or explicit constructor matching with the declaration of the compound object.
- When there is Type inheritance, the inherited Type must have a default implicit or explicit constructor (unless the inheriting Type has a constant copy-constructor, explicitly defined by user), and all this even if no object is constructed (compiler test on the only inheritance structure). This behavior appears to be specific to FreeBASIC.

From all the above, one can deduce 'golden rules' that avoid most of compilation errors and run-time bugs.

Golden rules for a code safer (at compile-time and run-time):
- If the user explicitly defines any constructor, he is very strongly advised to also define explicitly the default-constructor as well.
- If the user needs to explicitly define (with a non empty body) a copy-constructor or a copy-assignment operator or a destructor, it is better to define the 3 simultaneously (the known 'rule of three'), plus the default-constructor (rule above also applied).

From all the above and more specific cases (with inheritance), one can propose one 'maximizing rule' that allows a very safer operating.

Maximizing rule for a very safer code (at compile-time and run-time), but sometimes maximizing the real constraints:
- If the user needs to explicitly define any form of constructor procedure (including any form of copy-constructor) or any form of let-operator procedure or a destructor procedure, it is strongly recommended to define together the default-constructor and the standard copy-constructor and the standard let-operator and the destructor.
(these 4 explicit procedures are explicitly defined to always overload correctly the corresponding implicit operations from compiler)




3. Member access rights impact (on declaration of constructors, copy-constructors, copy-assignment operators, and destructors)
Access rights can be applied when declaring such member procedures, to prohibit the user from performing certain specific commands (from the outside of Type).

The default constructor, copy-constructor, copy-assignment operator, and destructor are the only member procedures which can have an implicit version built by the compiler.
So if one want to forbid their accesses by the user from the outside of the Type, it is necessary to overload these by explicit versions with restricted access rights (not Public) when declaring. In addition, such member procedures may have no implementation (no body defining) if they are never actually called in the program.



See also
Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode