I want to use a abstract class as Interface in C++ for reasons :) like this:
class Base{
public:
virtual bool foo() = 0;
int getValue() {return this->value;};
int compare(Base other) {
//calculate fancy stuff using Base::foo() and other given stuff through inheritance
return result;
}
protected:
int value;
};
class TrueChild: public Base{
public:
TrueChild(int value): Base() { this->value = value;}
bool foo() {return 1;}
//do stuff with value
};
class FalseChild: public Base{
public:
FalseChild(int value): Base() { this->value = value;}
bool foo() {return false;}
//do other stuff with value
};
But I can't pass Base as type in the compare method because it's a abstract class and I can't instantiate it. C++ complains with cannot declare parameter ‘first’ to be of abstract type ‘Base’
. How can I create a method that takes params with type of any class, which implements the Base class?
I know it is kind of similar to questions like this, this or this, but these answers don't help, because they doesn't talk how to use the Interface as generalized type in any way.
Thank you :)