2

I'm reading the book Inside C++ Object Model and learning about the model of virtual multi-inheritance.

I know that we need insert two kinds of information into each part of base class in a derived class object:
virtual function and virtual base class.

The book proposed two strategies:
1)Two pointers. One for virtual function, the other for virtual base class.
enter image description here
2) One pointer pointing at one table, which contains virtual function slots and virtual base class offsets. We use negative numbers to present the virtual base class offsets.
enter image description here

I think nothing's wrong just now.

Then the book says:

In general, the most efficient use of a virtual base class is that of an abstract virtual base class with no associated data members.

enter image description here I don't know why no data members in an abstract virtual base class can be more efficient.

The book online is here. Page 68 - 74 to know about more detail about my question.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • 2
    Where is the evidence? – Ed Heal Mar 09 '17 at 13:38
  • @EdHeal I added three captures. – Yves Mar 09 '17 at 13:45
  • I think you should add "for multi-inheritance", as subject is pretty misleading without mentioning that. – Slava Mar 09 '17 at 14:05
  • 1
    This look like some quite tiresome design. Also, why would Point3D inherit from Point2D? That would mean that every Point3D is a Point2D. Makes only sense if you consider them to be nothing but data holders, but not true classes with a concept and all. And why do they use pointers? – Aziuth Mar 09 '17 at 14:43
  • @Aziuth It is just an example, I know this example is weird. The book used it. – Yves Mar 09 '17 at 15:09
  • 1
    As an aside, you might find these compiler options helpful: MSVC's [`/d1reportSingleClassLayoutX`](https://blogs.msdn.microsoft.com/vcblog/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022/) and the undocumented full version `/d1reportAllClassLayout`, Clang's [`-Xclang -fdump-record-layouts`](http://eli.thegreenplace.net/2012/12/17/dumping-a-c-objects-memory-layout-with-clang/), and GCC's [`-fdump-class-hierarchy`](http://stackoverflow.com/questions/2549618/is-there-any-g-option-to-dump-class-layout-and-vtables). You can use them to see how compilers do it in practice. – Justin Time - Reinstate Monica Mar 09 '17 at 18:57
  • (Note that for `/d1reportSingleClassLayoutX`, you have to replace `X` with the name of the class whose layout you want to dump.) – Justin Time - Reinstate Monica Mar 09 '17 at 18:59

0 Answers0