Limitations on Constructors and Destructors in C++
Limitations on Constructors:-
The following are the limitations on constructor functions.
- Constructor functions should be declared in the public section of the class. Private and Protected constructors are available only to the member functions and friends of that class.
- A constructor function does not have any return type, not even void. Hence, constructor functions can’t return any value.
- A constructor function may not be static.
- We cannot fetch the address of a constructor function.
- Constructors cannot be made virtual.
- Constructors may not be static.
- Constructor functions cannot be inherited. However, a derived class can call the base class constructor.
- An object of a class with a constructor can’t be member of a union.
- The copy constructor and the default constructor are generated by the compiler only if these have not been declared for a class.
- Name of the constructor function must be same as that of the class.
Limitations on Destructors:
The following are the limitations imposed on destructor functions:
- Destructors neither take any argument nor do they return any value, not even void.
- Destructors cannot be inherited.
- It is not possible to refer to the address of a destructor.
- A destructor may not be static.
- An object of a class with a destructor cannot be a member of a union.
- A destructor function must have public access in the class declaration.
- A destructor name must be similar to that of a class, preceded by a tilde (~) symbol.