简体   繁体   English

C++ 用内部 Class 参数覆盖 Function

[英]C++ Overriding Function with Inner Class Argument

I have an abstract graph ( A in the example).我有一个抽象图(示例中的A )。 And it has inner-class InnerA which is used for one of the functions.它有内部类InnerA用于其中一个功能。 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 派生一个新的 class (例如B ),并在InnerA周围制作一个包装器,以添加一个应该在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.但是我收到一个错误, B中的function无法覆盖基础 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:考虑这个 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.对于有效的覆盖,这必须是对派生 class function 的有效调用,但事实并非如此。 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 derived class function must be able to handle all the calls to the base class function, by having the same arguments and return type.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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