VTABLE virtual function in derived class

R

rahul8143

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;
}
 
M

Maxim Yegorushkin

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

rahul8143

Maxim said:
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?
 
M

Maxim Yegorushkin

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top