In c++ there is only virtual destructors, no constructors. Why?

Answers were Sorted based on User's Feedback



In c++ there is only virtual destructors, no constructors. Why?..

Answer / uma sankar pradhan

constructors are invoked at the time of object creation.
The v-table for virtual function mechanism is not
intialised properly during the constructor invocation.
So constructors cannot be virtual

But,destructors can be virtual.Because the V-table is
properly initialised at the time of object creation.
So the v-table is available for the destructors.
Destructors can be made pure virtual also.But unlike other
member functions,it must be redfined outside the class.

Is This Answer Correct ?    57 Yes 22 No

In c++ there is only virtual destructors, no constructors. Why?..

Answer / bala preethi

Since OOPS basics says always base class object to be
created and than its derivatives. So just consider the case
where we try to create object of derived class with virtual
contructor since pointer points to derived type object it
fetches derived constructor directly, without its base
class object constructed( instantiated )...
So Constructor cannot be made virtual bt when comes to case
of Destructor object destuction process must be reverse...
So we must make destructor as virtual.
This applies only when we going for Dynamic Memory
Allocation...

If, on the other hand, you were to have a pointer to object
A and assign it a dynamic object B, when you destroy it,
only object A's destructor is called and object B part of
the object is never destroyed. Virtual destructors are used
to solve this problem and destroy the objects in the right
order.

Is This Answer Correct ?    47 Yes 14 No

In c++ there is only virtual destructors, no constructors. Why?..

Answer / subhashish sen

Constructor can not be virtual...The main reason is when we
have a virtual function in a class the VTABLE for that
function will be created..which contains the address of the
virtual function...means for each class which contains a
virtual function a VTABLE will be created and constructor is
an entity which initializes this VTABLE.So if you declare
constructor as virtual who will initialize VTABLE for it.

Destructors can be virtual because virtual destructor
ensures proper calling order of all destructors in a class
hierarchy.

If you need further clarification reply me....

Is This Answer Correct ?    28 Yes 9 No

In c++ there is only virtual destructors, no constructors. Why?..

Answer / jos collin

The VTable is created at compile-time itself. But properly
initialized at runtime, including vptr.

Virtual Mechanism is possible only with completely
constructed objects. The constructor is called when the
object is not created completely, it is not allowed.

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More OOPS Interview Questions

What is variable example?

0 Answers  


What is polymorphism and why is it important?

0 Answers  


create a class complex having real and imaginary part of a complex no. as a data member. overload the binary operators(+,- and *) to perform the operations on complex no. objects. overload binary operator using friend function.

2 Answers   CTS, Delhi University,


what is the difference between <stdio.h>and "stdio.h"?

5 Answers  


write a code for this:trailer recordId contains a value other than 99, then the file must error with the reason ‘Invalid RECORD_ID’(User Defined Exception).

0 Answers  






Why do while loop is used?

0 Answers  


What do you mean by overloading?

0 Answers  


What is the real time example of encapsulation?

0 Answers  


How to deploy web appliction in web logic ?

1 Answers   Unisys,


What are the OOPS concepts?

106 Answers   A1 Technology, Bajaj, CTS, EDS, HP, Infosys, Intel, Microsoft, MNC, Persistent, PlanetSoft, ProConstructor, TCS, Virtusa, Wipro, YSoft Solutions,


What is the renewal class?

0 Answers   Ebix, IBM,


What is a unary operator?

5 Answers  


Categories