I want to have different implementation hidden behind a unique interface.
I tried the answers to this question, but it seems that the only thing that it does is to force developers to implement certain methods.
Using the answer to the question mentioned, this is what I'd like to do.
#include <iostream>
using namespace std;
class Interface
{
public:
virtual ~Interface() {}
virtual void OverrideMe() = 0;
};
class Implementation1 : public Interface
{
public:
virtual void OverrideMe()
{
cout << "1" << endl;
}
};
class Implementation2 : public Interface
{
public:
virtual void OverrideMe()
{
cout << "2" << endl;
}
};
int main(int argc, char *argv[])
{
Interface a = Implementation1();
Interface b = Implementation2();
a.OverrideMe(); //expecting 1
b.OverrideMe(); //expecting 2
}
This does not compile, and I could find a way to have a type that allows different behavior based on the constructor.
Even if I add the Parent
class mentioned in the answer, the program does not compile.