Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of Polymorphic calls - C++

I'm writing a game in C++ that has about 30 different roles that are each slightly different. I have a main class User that contains all of the data required by all of the roles. My first implementation involved just having an enumeration of 30 roles and handling appropriately, but now I'm wondering if it would be better to have User as a base class and each role being its own class that inherits from User.

My main concern is how efficient are polymorphic method calls when there are 30+ classes inheriting from a single base class? I know polymorphic calls involve pointers in a virtual table but I'm not sure if that means a linear search through the entire table for the right method or if it can be done in constant time.

If anyone could comment on the efficiency of polymorphic method calls with a many inherited classes I would appreciate the enlightenment.

Thanks in advance!

like image 384
Josh Brittain Avatar asked Apr 15 '12 23:04

Josh Brittain


People also ask

What is polymorphism C+?

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.

How does C++ implement polymorphism?

You can implement compile-time polymorphism using function overloading and operator overloading. Method/function overloading is an implementation of compile-time polymorphism where the same name can be assigned to more than one method or function, having different arguments or signatures and different return types.

What is polymorphism and example?

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics.

Why do we need dynamic polymorphism?

Dynamic Polymorphism allows Java to support overriding of methods which is central for run-time polymorphism. It allows a class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods.


2 Answers

The cost is negligeable. It doesn't matter how many classes you have, or how many levels of inheritance, the cost of a polymorphic call is a simple addition - the pointer to the vftable plus the offset of the specific function (not standard mandated, but on most, if not all, implementations this is correct).

like image 193
Luchian Grigore Avatar answered Oct 14 '22 00:10

Luchian Grigore


Rather than using inheritance, use composition. Just give your 'User' class an object that does the role. So you may end up with 30 'Role' classes. But they don't inherit off 'User', but are given to 'User' to use ( they may inherit off their own base abstract class to define the interface of 'Role')

or if it is just one function.... you might just model it as a bunch of function objects and then just pass those to User.

like image 32
Keith Nicholas Avatar answered Oct 14 '22 01:10

Keith Nicholas