473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 13001
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
7
by: Oleksii | last post by:
Hello, I'm rather new to the advanced topics, therefore I cannot explain the following myself. Could anyone give me a hint on this one? I'm trying to avoid link-time dependencies on (a test...
4
by: rahul8143 | last post by:
hello, what happens to VTABLE when base class has virtual function and derived class does not override it? Does derived class also builds VTABLE if it has no functions? eg. if code is like...
12
by: sojin | last post by:
Hi all, I'm a new to c++... and I've some doubts on "virtual" topics. 1) Can abstract base class have V-table? Here's the way,, Class CTemp{
2
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.