c++ - 为什么在这种情况下不能访问基类的成员函数?

标签 c++ visual-studio-2010 visual-c++

我很困惑,为什么我无法访问 void func(int i),有人可以帮助我吗? 当然,这只是一个演示,可以帮助您轻松理解我的问题。其真正的代码量很大,我希望Base和Child中的成员函数都可用。

输出总是 **

double
2

**

        struct base
        {
            void func(int i)
            {
                cout << "int" << endl;
                cout << i << endl;
            }
        };

        struct child : base
        {
            void func(double d)
            {
                cout << "double" << endl;
                cout << d << endl;
            }
        };

        child c;
        c.func((int)2);

最佳答案

因为 child::func 隐藏 base::func

您需要通过将名称引入作用域使其在派生类中可见:

struct child : base
{
    using base::func;
     void func(double d)
     {
         cout << "double" << endl;
         cout << d << endl;
     }
};

或通过在调用站点限定名称来显式调用基本版本:

c.base::func(2);

关于c++ - 为什么在这种情况下不能访问基类的成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143707/

相关文章:

visual-studio - 名为 'NuGet' 的包源无效或不可用

visual-c++ - 如何使用 OpenCV 从我的磁盘播放视频文件?

c++ - 不能包含 "vcruntime_new_debug.h"

c++ - 如果对可推导类型进行替换,可变参数模板类型推导会使编译器崩溃

c++ - 尽管类型删除,是否可以使用静态多态性(模板)?

c++ - 什么是 glibc free/malloc/realloc invalid next size/invalid pointer error 以及如何修复它?

visual-studio-2010 - Azure VM角色问题

c# - 仅在 VS 2010 上运行时 : COMException: Catastrophic Failure: Error Code: -2147418113

c# - VS Express和专业有冲突吗?

c++ - 使用 EncodePointer/DecodePointer 的好处