Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Inheritance/VTable questions

Update: Replaced the destructor example with a straight up method call example.

Hi,

If I have the following code:

class a
{
public:
    virtual void func0(); // a has a VTable now
    void func1();
};
class b : public a
{
public:
    void func0() { a::func0(); }
    void func2();
};
  1. Is there a VTable in B? B has no virtual functions but calls a::func0() from b::func0()
  2. Does func1 reside in a VTable? It's not virtual.
  3. Does func2 reside in a VTable?
  4. Will the answers to the above be different if there wasn't a a::func0() call in b::func0()?

Thanks

like image 323
jameszhao00 Avatar asked Sep 05 '09 15:09

jameszhao00


People also ask

What is use of vtable in inheritance?

VTable structure used by the compiler to keep track of the virtual functions associated with a class. There is one instance of a VTable for every class containing virtual functions. All instances of a given class point to the same VTable.

Is vtable of a class can be inherited?

The vtable contains all inherited virtual functions and any newly introduced one.

How many VPTR and vtable will be created?

There is only one VPTR for each object when using simple inheritance like this. The VPTR must be initialized to point to the starting address of the appropriate VTABLE. (This happens in the constructor, which you'll see later in more detail.)

What is a vtable in C?

Client applications and service providers written in C define MAPI objects by creating a data structure and an array of ordered function pointers known as a virtual function table, or vtable. A pointer to the vtable must be the first member of the data structure.


1 Answers

If you declare virtual functions you should also declare your destructor virtual ;-).

  1. B has a virtual table, because it has a virtual function, namely func0(). If you declare a function (including a destructor) virtual in a base class, all its derived classes will have the function with same signature virtual as well. And it will cause them to have a vtable. Moreover, B would have the vtable even if you didn't declare func0 in it explicitly.

  2. Non-virtual functions are not referenced through vtables.

  3. See 2.

  4. No. Classes' vtables are constructed based on class declarations. The bodies of class' functions (let alone other functions) are not taken into account. Therefore, B has a vtable, because its function func0() is virtual.

There also is a tricky detail, although it's not the gist of your question. You declared your function B::func0() as inline. In gcc compiler, if a virtual function is declared inline, it retains its slot in virtual table, the slot pointing to a special function emitted for that inline one (that counts as taking its address, which makes the inline emitted). That means, whether the funciton is inline doesn't influence amount of slots in vtable and its necessity for a class.

like image 178
P Shved Avatar answered Oct 27 '22 02:10

P Shved