c++ switch vs. member function pointer vs. virtual inheritance

Viewed 2992

I am trying to analyze the trade offs between various methods of achieving polymorphism. I need a list of objects with some similarities and some differences in member functions. The options I see are as follows:

  1. have a flag in each object, and a switch statement in each function. The value of the flag directs each object to its specific section of each function.
  2. have an array of member function pointers in the object, which are assigned upon construction. Then, I call that function pointer to get the correct member function.
  3. have an virtual base class with several derived classes. One drawback to this is that my list will now have to contain pointers, and not the objects themselves.

My understanding is that the pointer lookups from the list in option 3 will take longer than the member function lookups of option 2 because of the guaranteed proximity of member functions.

What are some of the benefits/drawbacks of these options? My priority is performance over readability. Is there any other method for polymorphism?

3 Answers
Related