Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this work? C++ multiple inheritance and constructor chaining

Consider a class Waldo, that inherits from Foo and Baz, viz.:

class Waldo : public Foo, public Baz {
  ...
};

When I create a new instance of Waldo:

   Waldo *w = new Waldo;

do the Foo and Baz constructors get called? If they are not called by default, is there an easy way IN THE CLASS DECLARATION OR IN THE DECLARATION/DEFINITION of the Waldo constructor to force them to be called?

(It looks like I may be trying to do constructor chaining, and C++ allegedly doesn't do that. I'm not sure.)

What I'm trying to do is "annotate" various class declarations, where the annotations cause (among other things) instances of the annotated class to be linked into lists maintained by the annotation classes. This lets me, for example, walk the list of all objects with Baz-nature, applying a certain operation to them, and not have to worry about whether I remembered to add the instance to the list of objects with Baz-nature, because the constructor did it automagically.

like image 548
John R. Strohm Avatar asked Apr 16 '12 14:04

John R. Strohm


1 Answers

Yes, they are called. Left to right.

like image 103
Sebastian Mach Avatar answered Oct 19 '22 09:10

Sebastian Mach