0

A followup to this question : C++ : Is there a good way to choose between 2 implementations?

My goal here is that both of these classes can be called at the same runtime. So Polymorphism would be a good and easy way to go with.

I have the following diagram : enter image description here

But the problem here is that each one of them(a.cpp/h and a_mockup.cpp/h) uses the singelton pattern(contains static functions) and static functions cannot be virtually declared in the baseclass.

Is there a way to solve this problem?

Upgrade
  • 95
  • 6
  • 1
    Having static functions doesn't mean they're singletons. Singletons are evil. The solution is to never in your life touch singletons if you can help it. – Passer By Jan 23 '22 at 13:25
  • The only way to implement static polymorphism is to use a [CRTP](https://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp). – πάντα ῥεῖ Jan 23 '22 at 13:25

2 Answers2

0

In C++ Virtual functions are invoked during run-time, when you have a pointer/reference to an instance of a class. Static functions aren't bind to a particular instance, they're bind to a class during compile-time. Moreover, C++ doesn't have pointers-to-class functionality. So, there is no such way to solve this.

smalik
  • 343
  • 4
  • 9
  • 1
    As mentioned in my comment, the CRTP is a way, to achieve static polymorphism, where those class member calls are resolved at compile time. But as a consequence everything needs to be known at compile time as well. – πάντα ῥεῖ Jan 23 '22 at 13:37
0

You must have CRTP as your default design when aiming for static polymorphism.

Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76