简体   繁体   中英

C++ Overriding Function with Inner Class Argument

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 (eg 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 {
      ^~~~~~~~

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM