8

Here is the program on vtables. Am I understanding is correct on vtables and v-pointers.

Class B
{
  public:

  virtual Void Hello()
  {
    cout<<"Hello Base";
  }
};

class D: public B    
{
  public:

  virtual void Hello()
  {
    cout<<"Hello Derived";
  }
};

int main(int argc, char* argv[])
{
  D *d1 = new D();
  D *d2 = new D();
  D *d3 = new D();

  return 0;
}

In my opinion, there will be two vtables and only one vptr. Am I correct on it?

jww
  • 97,681
  • 90
  • 411
  • 885
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • Did you intend for D to inherit from B? – 2785528 Apr 19 '14 at 12:44
  • Oops, thanks! yes inheritance. – dexterous Apr 19 '14 at 12:46
  • 1
    This is too localized- ain't nobody gonna be helped by the answer to this question. Just read the ABI specification for your compiler- whichever compiler and ABI that is that you didn't specify- and it will define the answer. – Puppy Apr 19 '14 at 12:46
  • 2
    No vtables and no vptrs will be created by an implementation that uses no vtables, or by an implementation that eliminates vtables during a whole-program optimization pass. – n. m. could be an AI Apr 19 '14 at 12:59

3 Answers3

16

The standard does not define how virtual functions are actually implemented, only how they should behave. So what you are asking for entirely depends on the compiler you are using.


GCC will in theory most likely create two vtables (one for B and one for D) and three vptrs (one for each of the object instances d1, d2, d3).

Have a look here: http://en.wikipedia.org/wiki/Virtual_method_table

Danvil
  • 22,240
  • 19
  • 65
  • 88
  • How it will behave on gcc? – dexterous Apr 19 '14 at 12:50
  • Whilst this is true, it's also totally unhelpful and cannot accurately answer his question.Not that this is your fault since his question is unanswerable, but that's another matter. – Puppy Apr 19 '14 at 12:58
  • @DeadMG: Why is it unhelpful? I made a statement about the general case and gave some hints on a specific case. – Danvil Apr 19 '14 at 13:00
  • Compile it with gcc and see for yourself. – n. m. could be an AI Apr 19 '14 at 13:01
  • The statement about the general case is "Your question is unanswerable in the general case (but this is totally an answer for the general case anyway)", which we already knew, and his question does not identify a specific case. – Puppy Apr 19 '14 at 13:05
  • 1
    @DeadMG: It identifies the specific case of "an implementation using vtables and vptrs", which is enough for all but the most anal of pedants to answer the question. – Mike Seymour Apr 19 '14 at 13:45
8

You might find the implementation detail in your compiler documentation. It may vary from version to version, even between platforms.

For gcc on Linux, it is likely that each D instance will need its vptr and one vtable.

You have 3 instances of D:

  • Each will have its vptr -> 3 vptr
  • Each points to the same vtable.

So 3 vptr and 1 vtable (+1 vtable for B, which is useless here).

Again this is an implementation detail, and in your very simple case it is subject to compiler optimisations.

quantdev
  • 23,517
  • 5
  • 55
  • 88
  • You've answered an unanswerable question by answering some specific sub-case of it. That does not answer the question. – Puppy Apr 19 '14 at 13:34
  • 5
    I dont agree, the OP specified gcc, and gcc implementation is documented. Answering the question is helpful from a cultural perspective, even if you limit youself to a specific subcase of it. Anyway thanks for replying. – quantdev Apr 19 '14 at 13:41
5

In a typical vtable-based implementation of virtual polymorphism, there will be:

  • one vtable per class, containing the virtual function pointers and other metadata for that class;
  • one vptr per object, pointing to the vtable for that object's dynamic class type.

So here there will be two vtables (for B and D) and three vptrs (in *d1, *d2 and *d3). Unless the compiler notices that you're not using the objects an eliminates them altogether.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644