I’m finding it difficult to describe this problem very concisely, so I’ve attached the code for a demonstration program.
The general idea is that we want a set of Derived classes that are forced to implement some abstract Foo() function from a Base class. Each of the derived Foo() calls must accept a different parameter as input, but all of the parameters should also be derived from a BaseInput class.
We see two possible solutions so far, neither we’re very happy with:
-
Remove the Foo() function from the base class and reimplement it with the correct input types in each Derived class. This, however, removes the enforcement that it be implemented in the same manner in each derived class.
-
Do some kind of dynamic cast inside the receiving function to verify that the type received is correct. However, this does not prevent the programmer from making an error and passing the incorrect input data type. We would like the type to be passed to the Foo() function to be compile-time correct.
Is there some sort of pattern that could enforce this kind of behaviour? Is this whole idea breaking some sort of fundamental idea underlying OOP? We’d really like to hear your input on possible solutions outside of what we’ve come up with.
Thanks so much!
#include <iostream>
// these inputs will be sent to our Foo function below
class BaseInput {};
class Derived1Input : public BaseInput { public: int d1Custom; };
class Derived2Input : public BaseInput { public: float d2Custom; };
class Base
{
public:
virtual void Foo(BaseInput& i) = 0;
};
class Derived1 : public Base
{
public:
// we don't know what type the input is -- do we have to try to cast to what we want
// and see if it works?
virtual void Foo(BaseInput& i) { std::cout << "I don't want to cast this..." << std::endl; }
// prefer something like this, but then it's not overriding the Base implementation
//virtual void Foo(Derived1Input& i) { std::cout << "Derived1 did something with Derived1Input..." << std::endl; }
};
class Derived2 : public Base
{
public:
// we don't know what type the input is -- do we have to try to cast to what we want
// and see if it works?
virtual void Foo(BaseInput& i) { std::cout << "I don't want to cast this..." << std::endl; }
// prefer something like this, but then it's not overriding the Base implementation
//virtual void Foo(Derived2Input& i) { std::cout << "Derived2 did something with Derived2Input..." << std::endl; }
};
int main()
{
Derived1 d1; Derived1Input d1i;
Derived2 d2; Derived2Input d2i;
// set up some dummy data
d1i.d1Custom = 1;
d2i.d2Custom = 1.f;
d1.Foo(d2i); // this compiles, but is a mistake! how can we avoid this?
// Derived1::Foo() should only accept Derived1Input, but then
// we can't declare Foo() in the Base class.
return 0;
}
Since your
Derivedclass is-aBaseclass, it should never tighten the base contract preconditions: if it has to behave like aBase, it should acceptBaseInputallright. This is known as the Liskov Substitution Principle.Although you can do runtime checking of your argument, you can never achieve a fully type-safe way of doing this: your compiler may be able to match the
DerivedInputwhen it sees aDerivedobject (static type), but it can not know what subtype is going to be behind aBaseobject…The requirements
DerivedXshould take aDerivedXInputDerivedX::Fooshould be interface-equal toDerivedY::Foocontradict: either the
Foomethods are implemented in terms of theBaseInput, and thus have identical interfaces in all derived classes, or theDerivedXInputtypes differ, and they cannot have the same interface.That’s, in my opinion, the problem.
This problem occured to me, too, when writing tightly coupled classes that are handled in a type-unaware framework:
And a framework:
Now this proves a design that’s too easily broken: there’s no part in the design that aligns the trees with the eaters:
You may improve the cohesion of the design, and remove the need for virtual dispatching, by making it a template:
The implementations are really template specializations: