Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What do you mean by virtual destructor in C++ and when they are invoked??
What are the vtable and vptr entries in the object and how they are initialized??

Please Give some examples also..
Posted
Updated 11-Mar-13 1:13am
v3

There is no such thing as a deconstructor. It's called a destructor.

"If a method is virtual, then calling the method on an object always invokes the method as implemented by the most heavily derived class. If the method is not virtual, then the implementation corresponding to the compile-time type of the object pointer. "

http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx[^]
 
Share this answer
 
Construction always happens from base to derived. And destruction 'should' happen from derived to base. If you are not using virtual function mechanism, it will work as expected, but consider following example,

base *p = new derived();


Here compiler assumes that, p is 'base' type. So unless you make destructor virtual, it will not call derived's destructor. Which may led to memory leak.
 
Share this answer
 
Comments
BaldevS 11-Mar-13 8:18am    
i know these things but i want to know some internals of virtual function like vtable ,vptr, offset and function pointer in vtable,how they are initialized etc...
All of these entities only exist for classes that have virtual functions, or are derived from a class that has.

For non-virtual functions, the compiler can resolve any function call at compile time. It is translated into a jump to a different address of the code base. For virtual functions however, the function that has to be called depends on the class type of the object that the function is called on, and that type may not be known at compile time.

As a help to determine these things at run time, the compiler generates a vtable for each class with virtual functions (at compile time, obviously). That table contains function pointers to the methods of this class. At runtime, whenever an object of a class with virtual fucntions is created, it will be intialized with a pointer to that function table corresponding to its class. When a potentially virtual method is called on that object, the runtime system will follow the function table pointer and then look up the function within the referenced vtable.

The exact details of how the entries in a vtable and a vptr look like, depends on the compiler. It may vary for different versions of a compiler. It may even vary depending on the exact type of the class! As a programmer, you shouldn't care about that, unless you're developing a compiler yourself. In that case, you could try and check out the specific compiler documentation.
 
Share this answer
 
v3
Essentially, if a desctructor is declared virtual you can properly delete that object using a pointer of a base class type.

Generally if a class has some virtual functions, its desctructor should be made virtual.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900