简体   繁体   English

c ++多态性和虚函数

[英]c++ polymorphism and virtual function

Is it possible to call the virtual function foo( int ) from B without using what is done in comment ? 是否可以从B中调用虚函数foo(int)而不使用注释中的内容?

class A {
public: 

    virtual void foo ( char * ) {
    }

    virtual void foo ( int ) {
    }
};

class B : public A {
public:

    void foo ( char * ) {
    }

    //void foo ( int i ) {
    //  
    //  A::foo(i);
    //}
};

B b;
b.foo(123); // cannot convert argument 1 from 'int' to 'char *'

Yes, it is possible. 对的,这是可能的。 The problem here is that the function B::foo(char*) hides the name of the inherited function A::foo(int) , but you can bring it back into scope of B with a using declaration: 这里的问题是函数B::foo(char*) 隐藏了继承函数A::foo(int) ,但是你可以using声明将它带回到B范围内:

class B : public A {
public:

    void foo ( char * ) {
    }

    using A::foo;
};

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

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