Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does order of inheritance between class and interface matter?

Will it make any difference whether we inherit the class first or interface in C++?

example:

class A : public IAbstract, public ClassB
{
};

class A : public ClassB, public IAbstract
{
};
like image 815
Gilson PJ Avatar asked Aug 24 '16 08:08

Gilson PJ


2 Answers

The initialization order of direct base classes (i.e. ClassB and IAbstract) will be different. It's determined by the declaration order in base class specifier list.

(emphasis mine)

2) Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list

like image 126
songyuanyao Avatar answered Oct 04 '22 22:10

songyuanyao


Yes the object layout will be different. Functionally however, it's equivalent.

In the first case, the object layout will be something like this:

------
IAbstract members, including vptr
------
Class B members
------

And in the second case:

------
Class B members
------
IAbstract members, including vptr
------
like image 40
StoryTeller - Unslander Monica Avatar answered Oct 04 '22 21:10

StoryTeller - Unslander Monica