Constructors
For each object four (at least) constructors must be defined:
- default.
This constructor is declared as a private or protected member function,
making it accessible only to base and derived classes functions.
- copy
- from an xml file:
to perform the construction of an object given by an XML node. See XML component for more details.
- from a set of data that
characterized the object.
A few rules for constructor
implementation:
--> for base class:
- all members should be initialized in the constructor, as much as
possible in the constructor list (see example below)
- Set all the pointers to NULL.
- Memory allocation for all pointers only when the size is known
Example:
//The constructor list for members initialization ...
BaseClass::BaseClass(): objectMemberA(initValue),objectMemberB(initValue), size(valSize), objectPtr(0), ...
{
...
// Memory allocation
objectPtr = new TypeObjectPtr(size); ok
// objectPtr = new TypeObjectPtr(); // no!
...
}
-->for derived class: