468,720 Members | 1,689 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

VTABLE virtual function in derived class

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 this(only demo code)
class a{
public:
virtual void fun()
{
cout<<"Base class";
}
};
class d:public a
{
};

int main()
{
d obj;
return 0;
}

Aug 23 '05 #1
4 2819

rahul8...@gmail.com wrote:
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?


Why do you care?

In a typical implementation vtables are generated one per class by a
compiler at compile stage and stored in a read only segment of your
binary. Thus, no runtime vtable building occurs. A class instance bears
only a pointer to its vtable. Upon entering a constructor compiler
injected code sets the vtable pointer to a proper vtable.

Aug 23 '05 #2

Maxim Yegorushkin wrote:
rahul8...@gmail.com wrote:
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?
Why do you care?

In a typical implementation vtables are generated one per class by a
compiler at compile stage and stored in a read only segment of your
binary.

Does that mean if i have 3 classes base1, derived1, derived2 then
each one has VTABLE provided all are usinf virtual functions?Thus, no runtime vtable building occurs. A class instance bears that mean derived class also builds VTABLE at compile time then is
address od virtual function be same for base1 and derived1 if base1 has
virtual function and derived1 has no overridden function? only a pointer to its vtable. Upon entering a constructor compiler
injected code sets the vtable pointer to a proper vtable.


Aug 23 '05 #3

ra*******@gmail.com wrote:
Maxim Yegorushkin wrote:
rahul8...@gmail.com wrote:
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?
Why do you care?

In a typical implementation vtables are generated one per class by a
compiler at compile stage and stored in a read only segment of your
binary. Does that mean if i have 3 classes base1, derived1, derived2 then
each one has VTABLE provided
Yes. A compiler may optimize out a vtable for a derived class if it
does not overload any virtual functions so that its base class's vtable
is fine.
all are usinf virtual functions?
Did not understand this part.
Thus, no runtime vtable building occurs. A class instance bears that mean derived class also builds VTABLE at compile time then is
address od virtual function be same for base1 and derived1 if base1 has
virtual function and derived1 has no overridden function?


Yes.
only a pointer to its vtable. Upon entering a constructor compiler
injected code sets the vtable pointer to a proper vtable.


Aug 23 '05 #4

Maxim Yegorushkin wrote:

[]
that mean derived class also builds VTABLE at compile time then is
address od virtual function be same for base1 and derived1 if base1 has
virtual function and derived1 has no overridden function?


Yes.


Well, I am wrong here when multiple inheritance comes in. You might
find this illuminating http://files.rsdn.ru/4539/cud94.htm

Aug 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Karl Ebener | last post: by
9 posts views Thread by jimjim | last post: by
9 posts views Thread by kish_nand | last post: by
9 posts views Thread by schand | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.