C++ Overriding Function with Inner Class Argument

43 views Asked by At

I have an abstract graph (A in the example). And it has inner-class InnerA which is used for one of the functions. I want to derive a new class (e.g. B) from this class and make a wrapper around InnerA to add a new field that should be used in B.

class A {
public:
    class InnerA {};
    virtual void function(InnerA) = 0;
};

class B : public A {
public:
    class InnerB : InnerA {
    public:
        int new_field = 0;
    };
    void function(InnerB b) override {
        b.new_field = 1;
    }
};

But I receive an error that function in B cannot override the base function. What is the right way to implement this?

error: 'void B::function(B::InnerB)' marked 'override', but does not override
     void function(InnerB b) override {
      ^~~~~~~~
1

There are 1 answers

0
user253751 On

The argument type is different so this is not a valid override.

Consider this function:

void func(A* a) {
    A::InnerA innerA;
    a->function(innerA);
}

For a valid override, this would have to be a valid call to the derived class function, which it is not. The derived class function must be able to handle all the calls to the base class function, by having the same arguments and return type.