Discussion:
Number of Vtables in derived:base class
(too old to reply)
RAGHU
2009-11-27 09:46:40 UTC
Permalink
Hi all,


The below example shows a simple virtual function implementation.Where
only base class-"Window" is having a virtual function."Any class
having virtual function a Vtable will be created to that class".

Confusion:
Now I am inheriting the base class-window
class CommandButton : public Window

Now I would like know does for derived class "CommandButton" another
Vtable will be created or not??As you can see there are no virtual
functions in derived class.

class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual
function example
{
cout <<"Base class Window"<<endl;
}
};

class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++
virtual function"<<endl;
}
};

void main()
{
Window *x, *y;

x = new Window();
x->Create();

y = new CommandButton();
y->Create();
}


The output of the above program will be,
Base class Window
Derived class Command Button

Thanks,
RAGHU
Ulrich Eckhardt
2009-11-27 09:58:51 UTC
Permalink
Post by RAGHU
The below example shows a simple virtual function implementation.Where
only base class-"Window" is having a virtual function."Any class
having virtual function a Vtable will be created to that class".
Now I am inheriting the base class-window
class CommandButton : public Window
Now I would like know does for derived class "CommandButton" another
Vtable will be created or not??As you can see there are no virtual
functions in derived class.
[... Note: code shortened a bit... ]
Post by RAGHU
class Window // Base class for C++ virtual function example
{
virtual void Create();
};
class CommandButton : public Window
{
void Create();
};
Let's assume the derived class did not get its own vtable. In that case, it
would have the same vtable as the baseclass and a call to Create() would
call Window::Create(). Since it doesn't, obviously the derived class must
have a different vtable.

Note: If the derived class had more virtual functions, the vtable can not
remain the same (different size). Also, if the derived class has overridden
a virtual function, the vtable must change (different content).


BTW: Why do you care?

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Goran
2009-11-27 12:35:27 UTC
Permalink
Post by RAGHU
Now I am inheriting the base class-window
class CommandButton : public Window
Now I would like know does for derived class "CommandButton" another
Vtable will be created or not??As you can see there are no virtual
functions in derived class.
You are mistaken, yes, there is a virtual function (Create). It does
not matter that you didn't specify "virtual", it's virtual because
it's virtual in the base class.

Now, how virtual functions are implemented depends on the
implementation (compiler), but most often (and AFAIK always), it's
through a virtual function table. So, for MS compiler, your code will
have two vtables, for Window and for CommandButton.

Goran.

Loading...