Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does several levels of base classes slow down a class/struct in c++?

Tags:

c++

oop

Does having several levels of base classes slow down a class? A derives B derives C derives D derives F derives G, ...

Does multiple inheritance slow down a class?

like image 781
Brian R. Bondy Avatar asked Sep 19 '08 04:09

Brian R. Bondy


People also ask

Can a class inherit from multiple base classes?

Answer. Yes, a class can inherit from more than one base class. The term for this is “multiple inheritance”. When inheriting from a single class, the base class is included in parenthesis as part of the class definition following the class name.

How many base classes can a derived class have?

A derived class can have only one direct base class.

What is multiple base class?

A class can be derived from more than one base class. In a multiple-inheritance model (where classes are derived from more than one base class), the base classes are specified using the base-list grammar element.

Are structs faster than classes in C++?

To answer your question, struct is slightly faster.


1 Answers

Non-virtual function-calls have absolutely no performance hit at run-time, in accordance with the c++ mantra that you shouldn't pay for what you don't use. In a virtual function call, you generally pay for an extra pointer lookup, no matter how many levels of inheritance, or number of base classes you have. Of course this is all implementation defined.

Edit: As noted elsewhere, in some multiple inheritance scenarios, an adjustment to the 'this' pointer is required before making the call. Raymond Chen describes how this works for COM objects. Basically, calling a virtual function on an object that inherits from multiple bases can require an extra subtraction and a jmp instruction on top of the extra pointer lookup required for a virtual call.

like image 195
Eclipse Avatar answered Sep 19 '22 04:09

Eclipse