Limitations on Constructors and Destructors in C++

Limitations on Constructors and Destructors in C++

Limitations on Constructors:-

The following are the limitations on constructor functions.

  1. 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.
  2. A constructor function does not have any return type, not even void.  Hence, constructor functions can’t return any value.
  3. A constructor function may not be static.
  4. We cannot fetch the address of a constructor function.
  5. Constructors cannot be made virtual.
  6. Constructors may not be static.
  7. Constructor functions cannot be inherited.  However, a derived class can call the base class constructor.
  8. An object of a class with a constructor can’t be member of a union.
  9. The copy constructor and the default constructor are generated by the compiler only if these have not been declared for a class.
  10. 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:

  1. Destructors neither take any argument nor do they return any value, not even void.
  2. Destructors cannot be inherited.
  3. It is not possible to refer to the address of a destructor.
  4. A destructor may not be static.
  5. An object of a class with a destructor cannot be a member of a union.
  6. A destructor function must have public access in the class declaration.
  7. A destructor name must be similar to that of a class, preceded by a tilde (~) symbol.