Alex Rivera | Logout

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

Asked 2008-09-19T04:01:59.073
19

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?

Edit
Report

2 Answers

0

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.

answered 2008-09-19T04:06:58.983
0

As pointed out by Corey Ross the vtable is known at compile time for any leaf derived class, and so the cost of the virtual call really should be the same irrespective of the structure of the hierarchy.

This, however, cannot be said for dynamic_cast. If you consider how you might implement dynamic_cast, a basic approach will be have an O(n) search through your hierarchy!

In the case of a multiple inheritance hierarchy, you are also paying a small cost to convert between different classes in the hierarchy:

sturct A { int i; };
struct B { int j; };

struct C : public A, public B { int k ; };

// Let's assume that the layout of C is:  { [ int i ] [ int j ] [int k ] }

void foo (C * c) {
  A * a = c;                // Probably has zero cost
  B * b = c;                // Compiler needed to add sizeof(A) to 'c'
  c = static_cast<B*> (b);  // Compiler needed to take sizeof(A)' from 'b'
}
answered 2008-09-19T11:41:32.530

Your Answer