Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why not virtual ctors?? 2

Status
Not open for further replies.

ila

Programmer
Apr 3, 2001
9
FR
Hi,

why cant you make a CTOR virtual??
if there is a need to make a CTOR virtual..
what is stopping C++ from giving this option??

thanks
lak

 
A class can require something necessary operations, which you mustn't turn off. John Fill
 
John,

Thats what i am asking..
what are the necessary operations which block this
functionality??

Thanks
lak
 
It is up to whome has implemented this base class. The programmer who implements a inherited class usualy do not know what to initialize, which variable to change and many details. He know only some properties of it.
This mecanisme assure what the base class will be properly initialized. John Fill
 

The responsibility for creating an object of a class is
with the class's constructor. If you dont know, C++ compilers always create a default constructor if the programmer doesnt specify one in his declaration.

Declaring a constructor as 'virtual' violates the integrity of a class, since the derived class can then make changes in
the way the base class is constructed. Hence it is never
allowed.

In short, constructors are never virtual to make sure base classes are properly created(constructed). On the other hand destructors can be virtual or pure virtual.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top