Noona
Noona

Reputation: 643

class instance pointers or function pointers?

This is a c++ question. We would like to have 2 utility functions that have different implementations depending on a certain parameter, during runtime it is determined which implementation should be called based on this parameter, what design could be best in terms of memory usage and performance? We are thinking of two approaches but we can’t determine the improvement gained in either: - Defining an interface for these 2 utility functions and having multiple classes extending them, then we create a map with instances of these class implementations (eager initialising) - define all these functions in one class as static functions and invoke them using function pointers

Upvotes: 1

Views: 73

Answers (1)

Agent_L
Agent_L

Reputation: 5411

Virtual inheritance is usually realized using function pointers, so both of your ideas boil down to the same thing (from compiler point of view).

On a second thought, you are considering performance of a something as basic as function call. Are you 100% sure you're optimizing the part that is the bottleneck? It's extremely easy to get sidetracked when optimizing, and spend days on something which has 0 or 1% impact on performance. So stick to the golden rule: prove which part really slows you down. If you write tests for it, it'll be easy to test both solutions and get the results which one is faster.

Upvotes: 2

Related Questions