0

A simple question - how does the performance impact the extra calls when using aggregation stack against the performance impact from inheritance and virtual methods?

dtech
  • 47,916
  • 17
  • 112
  • 190
  • What about inheritance *without* virtual methods? – Bo Persson Jan 11 '12 at 16:22
  • ^ Wouldn't that cripple polymorphism and harm the flexibility of the class hierarchy? – dtech Jan 11 '12 at 16:24
  • 1
    Yes, but if aggregation works why do you need virtual functions? You cannot add virtual functions for "flexibility" and then complain if that hurts performance. This is comparing apples to oranges. – Bo Persson Jan 11 '12 at 16:26
  • No complaining, just asking about the cost of different techniques so I can find the sweet spot for my purposes. – dtech Jan 11 '12 at 16:44

1 Answers1

1

Virtualism has a little runtime overhead compared to any other statically resolved function calls but if this overhead is big enough can only be determined through profiling.

This recent answer of mine explains this in more detail.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • So in general the performance impact of virtualism is constant while with aggregation it is proportional to the depth? That would make inheritance the way to go in cases of deep trees of related hierarchy of classes, and aggregation more appropriate in more shallow scenarios? – dtech Jan 11 '12 at 16:21
  • 2
    Don't bother about performance overheads,If your design needs Inheritance use Inheritance if it needs Aggregation use Aggregation – Alok Save Jan 11 '12 at 16:24