I find it difficult to find (which I am sure is very common) a design template to solve the following problem. Consider this piece of code:
class AA {}; class BB : public AA {}; class A { public: virtual void foo(AA& aa) = 0; }; class B : A { public: void foo(BB& bb){cout<<"B::foo"<<endl;} }; int main() { B b; BB bb; b.foo(bb); }
This code will not compile because class B does not override the pure virtual function 'foo'. The compiler considers foo that B declares only as an overload for foo, because codimension is not allowed in the input parameters when overriding functions.
Now I understand the reason for this. The fact that B inherits from A means that it should be able to handle any calls to foo with parameters of type AA, and the previous code did not implement any type of parameter except BB.
Of course, I could just include aa in BB in the implementation of B foo, but I'm looking for a solution that preserves type safety and actually forces the class B implementer to also implement the class that inherits from AA in order to compile the code. In an ideal world, I could write something similar to this pseudo code:
class A { public: abstract class AA{};
Is there a way to achieve something like this in C ++? (possibly using some kind of matching object without the need for inheritance)
Note that in fact (as opposed to the example), inheritance between BB and AA is crucial, since AA has many children who have many qualities, and in the end, what I want to do is iterate over the class vector A and run 'foo' with appropriate parameters only (vector AA)
source share