Re: virtual table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Nov 20, 2013 at 6:53 AM, Riccardo Manfrin
<riccardomanfrin@xxxxxxxxx> wrote:
>
> suppose I have three classes Foo, Bar, Zoo.
>
> Foo inherits from Bar,
> Bar inherits from Zoo.
>
> Bar and Zoo are abstract classes (as defined here :
> http://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes)
>
> The size of Foo shall account for the two pointers to Bar an Zoo vtables.

For this kind of discussion it helps to show actual code.  If I
understand your description correctly, then your statement about the
size of Foo is incorrect.

#include <iostream>

class Zoo {
  virtual void zoo() = 0;
};

class Bar : public Zoo {
  virtual void bar() = 0;
};

class Foo : public Bar {
  void* v;
};

int
main()
{
  std::cout << sizeof(Foo) << std::endl;
}


On my x86_64 system this prints 16: 8 for the single vtable pointer, 8
for the void* field.  In other words, although Foo inherits from two
classes as you suggest, there is still only one vtable pointer.

Ian




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux