Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 1-Jan-13 7:57am
BaldevS379
Edited 11-Mar-13 1:13am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
BaldevS at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 645
1 OriginalGriff 335
2 CPallini 250
3 PIEBALDconsult 150
4 Fredrik Bornander 130
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,198
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,470


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100