468,241 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

virtual function and vtable

Could someone please explain me the concept behind virtual functions
and vtables. I am little confused about this. Please refer to the
following code and tell me how many virtual tables would be created and
what they would contain:

class base
{
virtual void display()
{
cout<<"base display"<<endl;
}
void disp()
{
cout<<"base disp"<<endl;
}
};

class d1: public base
{
void display()
{
cout<<"d1 display"<<endl;
}
};

class d2: public base
{
void display()
{
cout<<"d2 display"<<endl;
}
};

void main()
{
base *b = new d1;
d1->display();
}

Thanking you,

regards

Nand Kishore

Oct 3 '05 #1
9 12659
ki*******@yahoo.com wrote:
Could someone please explain me the concept behind virtual functions
and vtables. I am little confused about this. Please refer to the
there is no concept of vtable in c++, its up to compiler and its 'magic'
how virtual function are actually implemented
following code and tell me how many virtual tables would be created and
what they would contain:

#include <iostream>
using std::cout;
class base
{ public: virtual void display()
{
cout<<"base display"<<endl;
}
void disp()
{
cout<<"base disp"<<endl;
}
virtual ~base() {}

//you want vitrual destructor here - if you delete pointer of type base*
which points to object of class derived from base, destructor of class
base would be called only if you wont provide virtual destructor in base
};

class d1: public base
{ public: void display()
{
cout<<"d1 display"<<endl;
}
};

class d2: public base
{ public: void display()
{
cout<<"d2 display"<<endl;
}
};

//by the way, using struct instead of class would save you writing
'public' everywhere in your example

//> void main()
//you cant have void main(), its always 'int main'

int main() {
base *b = new d1;
//> d1->display();
//d1 is a type, you probably wanted
b->display();

delete b;
//event though main do return int you dont have to return it explicitly
as 'return 0;' is assumed }

Thanking you,

regards

Nand Kishore

Oct 3 '05 #2
I was talking about the vtable which are used to resolve the function
calls at runtime and not of virtual destructor. void main() is also
legal and its absolutely fine to have void main, the compiler won't
give you any error.

Oct 3 '05 #3
> ki*******@yahoo.comwrote:
Could someone please explain me the concept behind virtual functions
and vtables. Virtual functions are meant for dynamic binding.
As per my understanding , when we declare a function
as virtual , compiler will create vtable per class. vtable consists
addresses
of these virtual functions.
For the below program,
vtable for class base contains only address for display().

In the d1 class , vtable contains address for d1::display()
if this funtion is not overloaded in the derived class , then
content of vtable would be base::display()

Same things happen in d2 class.

I also have least idea about ,how compiler really resolves address of
these
virtual functions at run time
I am little confused about this. Please refer to the
following code and tell me how many virtual tables would be created and what they would contain:
Always , there will be only one vtable per class and these vtable
contain the addresses of virtual functions.

class base
{
virtual void display()
{
cout<<"base display"<<endl;
}
void disp()
{
cout<<"base disp"<<endl;
}
};

class d1: public base
{
void display()
{
cout<<"d1 display"<<endl;
}
};

class d2: public base
{
void display()
{
cout<<"d2 display"<<endl;
}
};

void main()
{
base *b = new d1;
d1->display();
}

Thanking you,

regards

Nand Kishore


Oct 3 '05 #4
Thanks for your reply.

So, will there be 3 vtables for the above code, one for base, one for
d1 and one for d2. Or just one for the base class.

Oct 3 '05 #5
ki*******@yahoo.com wrote:
I was talking about the vtable which are used to resolve the function
calls at runtime and not of virtual destructor. void main() is also
legal and its absolutely fine to have void main, the compiler won't
give you any error.


it just means that your compiler is broken 'void main' is illegal,
try this compiler (one of the most standard compiliant) for compiling
short snippets

http://www.comeaucomputing.com/tryitout/

and if you just wanted to understand vtable mechanism im sorry, i merely
corrected your code to get expected behaviour
Oct 3 '05 #6
ki*******@yahoo.com wrote:
So, will there be 3 vtables for the above code

Why do you need to know this? It is an implementation detail of the
compiler. Some compilers might make 3, some compilers might make a
hundred, some compilers might not even bother with virtual tables.

All the C++ programmer needs to know is that when a virtual function is
called, the most-derived override gets invoked. That's it. The rest
is magic.

If you really want to know how virtual tables are typically
implemented, then try comp.compilers

Hope this helps,
-shez-

Oct 3 '05 #7
Thanks a lot for your answer.

I am really not very much interested what happens behind the curtain. I
just wanted to know because some of the interviewer ask that type of
questions, so knowing that is not harm at all. I never knew the concept
of vtables before one interviewer asked me that. All i knew was how to
use virtual function to implement dynamic polymorphism.

Oct 3 '05 #8
>I was talking about the vtable which are used to resolve the function
calls at runtime and not of virtual destructor.
A good resource is Stan Lippman's Inside the C++ Object Model.
void main() is also
legal and its absolutely fine to have void main, the compiler won't
give you any error.


Mc's never warn you about the risk of cancer when selling you french fries.
Compiler writers surely don't want to lose their money by giving errors to
every program written before the formal C++ standardization.

Ben
Oct 3 '05 #9
Kyle <in*****@e.mail> wrote:
: ki*******@yahoo.com wrote:
: > Could someone please explain me the concept behind virtual functions
: > and vtables. I am little confused about this. Please refer to the

: there is no concept of vtable in c++, its up to compiler and its 'magic'
: how virtual function are actually implemented

: > following code and tell me how many virtual tables would be created and
: > what they would contain:
: >

Another poster wisely advised Lippman: his text "Inside the C++ Object
Model" describes this issue. Ti summarize, vtables are created (by some
compilers) on a per-class basis, so 3 vtables would result.

Feb 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kapil Khosla | last post: by
4 posts views Thread by vijay | last post: by
7 posts views Thread by Oleksii | last post: by
4 posts views Thread by rahul8143 | last post: by
2 posts views Thread by junw2000 | last post: by
23 posts views Thread by Dave Rahardja | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.