Does several levels of base classes slow down a cl

2020-02-26 05:04发布

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?

标签: c++ oop
12条回答
Bombasti
2楼-- · 2020-02-26 05:26
  • Does multiple inheritance slow down a class?

As mentioned several times, a deeply nested single inheritance hierarchy should impose no additional overhead for a virtual call (above the overhead imposed for any virtual call).

However, when multiple inheritance is involved, there is sometimes a very slight additional overhead when calling the virtual function through a base class pointer. In this case some implementations have the virtual function go through a small thunk that adjusts the 'this' pointer since

(static_cast<Base*>( this) == this)

Is not necessarily true depending on the object layout.

Note that all of this is very, very implementation dependent.

See Lippman's "Inside the C++ Object Model" Chapter 4.2 - Virtual Member Functions/Virtual Functions under MI

查看更多
男人必须洒脱
3楼-- · 2020-02-26 05:29

If there are no virtual functions, then it shouldn't. If there are then there is a performance impact in calling the virtual functions as these are called via function pointers or other indirect methods (depends on the situation). However, I do not think that the impact is related to the depth of the inheritance hierarchy.

Brian, to be clear and answer your comment. If there are no virtual functions anywhere in your inheritance tree, then there is no performance impact.

查看更多
迷人小祖宗
4楼-- · 2020-02-26 05:31

Yes, if you're referencing it like this:

// F is-a E,
// E is-a D and so on

A* aObject = new F(); 
aObject->CallAVirtual();

Then you're working with a pointer to an A type object. Given you're calling a function that is virtual, it has to look up the function table (vtable) to get the correct pointers. There is some overhead to that, yes.

查看更多
▲ chillily
5楼-- · 2020-02-26 05:38

Having non-trivial constructors in a deep inheritance tree can slow down object creation, when every creation of a child results in function calls to all the parent constructors all the way up to the base.

查看更多
混吃等死
6楼-- · 2020-02-26 05:39

While I'm not completely sure, I think that unless you're using virtual methods, the compiler should be able to optimize it well enough that inheritance shouldn't matter too much.

However, if you're calling up to functions in the base class which call functions in its base class, and so on, a lot, it could impact performance.

In essence, it highly depends on how you've structured your inheritance tree.

查看更多
叛逆
7楼-- · 2020-02-26 05:41

[Deep inheritance hierarchies] greatly increases the maintenance burden by adding unnecessary complexity, forcing users to learn the interfaces of many classes even when all they want to do is use a specific derived class. It can also have an impact on memory use and program performance by adding unnecessary vtables and indirection to classes that do not really need them. If you find yourself frequently creating deep inheritance hierarchies, you should review your design style to see if you've picked up this bad habit. Deep hierarchies are rarely needed and almost never good. And if you don't believe that but think that "OO just isn't OO without lots of inheritance," then a good counter-example to consider is the [C++] standard library itself. -- Herb Sutter

查看更多
登录 后发表回答