- Everything you wanted to know about constructors
- Constructors are used to initialize objects.
- When a class derives from other classes, the constructors are called in the following order.
- Virtual base classes, left to right in the class decleration
- base classes, left to right in the class decleration
- Member classes and references
- Finally, the body of the constructor is executed.
- A copy constructor is a constructor which takes a const reference of type self and is used to construct a copy of the instance. A copy constructor is used extensively by STL containers and algorithms.
- A constructor must not leak memory, even if any of the constructors of base or member classes throws an exception.
- A default constructor is a constructor which takes no arguments, or a constructor which has default values for all arguments. If we dont define any constructor in our class, then the compiler automatically creates a default constructor for our class
- If we make the constructor of a class private, then an instance of the class can not be created directly. We often use this to create a singleton pattern.
- If you have a reference member, then the only way for you to initialize the reference is in the constructor. If your class has pointers, then you should always initialize the pointer to NULL in the constructor.
Wednesday, July 29, 2009
Everything about constructors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment