virtual function and vtable

K

kish_nand

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
 
K

Kyle

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:


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
 
K

kish_nand

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

Bangalore

(e-mail address removed):
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.
 
K

kish_nand

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

Kyle

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
 
S

Shezan Baig

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-
 
K

kish_nand

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

benben

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
 
S

Scott J. McCaughrin

: (e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top