简体   繁体   English

C ++中的多态性为什么这不起作用?

[英]Polymorphism in C++ why is this isn't working?

class Base {
    public:
    virtual void f();
    void f(int);
    virtual ~Base();
};

class Derived : public Base {
public:
    void f();
};

int main()
{
    Derived *ptr = new Derived;
    ptr->f(1);
    delete ptr;
    return 0;
}

ptr->f(1); ptr-> F(1); is showing the following error: "too many arguments in function call". 显示以下错误:“函数调用中的参数太多”。

Why is this isn't possible? 为什么这是不可能的? isn't derived inherited all the functions form base and is free to use any of them? 是不是派生继承了所有的函数形式基础并可以自由使用它们中的任何一个? I could call it explicitly and it would work but why isn't this allowed? 我可以明确地称之为它会起作用,但为什么不允许这样做?

What you are seeing is called hiding . 你所看到的被称为隐藏

When you override the function void f() in the Derived class, you hide all other variants of the f function in the Base class. 当您在Derived类中重写函数void f()时,将隐藏 Base类中f函数的所有其他变体。

You can solve this with the using keyword: 您可以using关键字解决此问题:

class Derived : public Base {
public:
    using Base::f;  // Pull all `f` symbols from the base class into the scope of this class

    void f() override;  // Override the non-argument version
};

As mentioned by @Some Programming Dude : it is because of Hiding. 正如@Some Programming Dude所说:这是因为隐藏。

To understand hiding in relatively simpler language 要理解隐藏在相对简单的语言中

Inheritance is meant to bring Base class variables / functions in Derived class. Inheritance是为了在Derived类中引入Base类变量/函数。

But, on 1 condition : "If its not already available in Derived Class" 但是,在一个条件下:“如果它尚未在派生类中可用”

Since f() is already available in Derived , it doesn't make sense to look at Base class from compiler perspective. 由于f()已在Derived可用,因此从编译器角度查看Base类是没有意义的。

That's the precise reason why you need to scope clarify while calling this function 这就是为什么在调用此函数时需要调整范围的确切原因

void main()
    {
        Derived *ptr = new Derived;
        ptr->Base::f(1);
        delete ptr;
    }

Suppose for time being that Derived do have the access to the function void Base::f(int); 假设Derived确实可以访问函数void Base::f(int); . Then it will be a case of function overloading . 那么它将是一个function overloading的情况。 But, this is invalid case of function overloading since one function f(int); 但是,这是function overloading无效情况,因为一个函数f(int); is in Base and other function f(); 在Base和其他函数f(); is in Derived. 在Derived中。 Function Overloading happens inside a single class. Function Overloading发生在单个类中。 Function Overriding happens across classes. Function Overriding跨类发生。 The example you posted is a case of Name Hiding in Inheritance 您发布的示例是名称隐藏在继承中的情况

"Derived *ptr" This definition will only allow "ptr" to access all the member functions which are defined via Derived class or its child class. “Derived * ptr”这个定义只允许“ptr”访问通过Derived类或其子类定义的所有成员函数。 But, it will not allow u to access the member functions which are coming in Derived class because of inheritance. 但是,由于继承,它不允许你访问Derived类中的成员函数。

If u want to access base class version of function "f" then use "Base *ptr" and it will choose the correct version of function automatically as shown :) 如果你想访问函数“f”的基类版本然后使用“Base * ptr”,它会自动选择正确的函数版本,如图所示:)

class Base {
    public:
        virtual void f()
        {
            cout<<"Here 2"<<endl;
        }
        void f(int x)
        {
            cout<<"Here 1"<<endl;
        }
        virtual    ~Base() {}
};

class Derived : public Base {
    public:
        void f()
        {
            cout<<"Here 3"<<endl;
        }
        virtual   ~Derived() {}
};

int main()
{
    Base *ptr = new Derived;
    ptr->f(1);
    delete ptr;
    return 0;
}

output is Here 1 输出在这里1

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

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