Showing posts with label virtual table. Show all posts
Showing posts with label virtual table. Show all posts

20130330

How to see vtable virtual table for virtual functions in cpp


Virtual table is look up table for virtual functions. It contains the function pointer to the virtual functions. Virtual table is created for each class having one or more virtual function and classes derived from that class.virtual pointer better known as vptr containing the address of vtable is created for the most base class thats why size of empty class with virtual function is equal to the size of a pointer(8 bytes for a 64 bit computer)while size of a empty class without any virtual functions is only one byte.
Command to see vtable/virtual table of a class.
Here if the name of the program is "classWithVf.cxx" so the command to see the vtable of the class is:

g++ -fdump-class-hierarchy classWithVf.cxx


#include<iostream>
using namespace std;

class M //empty class
{
public:
void f() {}
};
class A //empty class
{