UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2004-05-05 07:15 AM

kautish
Guest

Re: vtables

Hi,

I have the following code sample:

class A
{
public:
         virtual void a1() { cout << "I am a1" << endl;}
         virtual void a2() { cout << "I am a2" << endl;}
};

class B
{
public:
         virtual void b1() { cout << "I am b1" << endl;}
         virtual void b2() { cout << "I am b2" << endl;}
};

class C : public A, public B
{
public:
         virtual void c1() { cout << "I am c1" << endl;}
         virtual void c2() { cout << "I am c2" << endl;}
};

myfunc(B* b)
{
        b->b1();
}

main()
{
          C* c = new C();
          myfunc(c);
}

Question:
How many vtable would be present in C class?

#2 2004-05-13 12:47 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: vtables

That sounds like a class / course question...  but its been a week so...

Typically, once a class has any ( 1 or more ) function declared virtual, a v-table is created for that class.  Inheritance from other class(es) with virtual functions does not affect this since they contain their own v-table(s)...

Btw, you base classes in the example are missing virtual destructor(s)... very very bad if something like that was created / deleted on the heap... lol

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2004-08-11 12:47 PM

biswajitnanda
Member
From: Bangalore
Registered: 2004-08-03
Posts: 6
Website

Re: vtables

Only one v table will be created for a class, no matter how many functions u hv defined in it as virtual...


Biswajit Nanda
S/W Eng, Venture Infotek Global Pvt. Ltd.
Bangalore, India

Offline

#4 2005-03-30 06:36 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: vtables

Yes, only 1 virtual table per class which contains ( any number of ) virtual functions...

OR

inherits from one or more classes classes which contain ( any number of ) virtual functions...


Using psuedo name mangling to id and represent the pointers and using the code in the prior msg:

Class A would have the v-table:

A_a1
A_a2

Class B would have the v-table:

B_b1
B_b2

Class C would have the v-table:

A_a1
A_a2
B_b1
B_b2
C_c1
C_c2


Btw, the reason I am writing this is because a reader recently emailed me about the line in my previous response where I said:

"Inheritance from other class(es) with virtual functions does not affect this since they contain their own v-table(s)". 

Re-reading it, that comment is understandably confusing and not very well written.  Oops.  The point I was trying to make was that class C does not have to maintain full copies of the class A & B individual vtables inside itself but can instead ( using mangling / whatever ) use a single table to hold all the information.


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB