为什么在 SO SIUTATION 中无法访问基类的成员函数?
why can't access member function of base class in thus siutation?
我很困惑,为什么我无法访问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);
从 int 到 double 的隐式转换掩盖了实际问题。如果将基类 func 参数类型从 int 更改为字符串:
struct base
{
void func(string i)
{
cout << "string" << endl;
cout << i << endl;
}
};
然后,您将收到以下错误以使其更清晰:
func.cpp: In function `int main()':
func.cpp:27: error: no matching function for call to `child::func(const char[13])'
func.cpp:17: note: candidates are: void child::func(double)
在你可以看到它的地方只有子::func而不是base::func的可见性::func
相关文章:
- 派生类看不到基类成员
- C++11: 如何访问派生类中的基类成员?
- Qml 未收到基类成员变量的更新值
- 指向从指针派生类成员函数的指针,指向基类成员函数
- 初始化基类成员 (c++) 的首选方法
- 访问多级复合关系中的基类成员
- 允许从特定派生类访问基类成员
- 在派生类的成员联合中包含继承的基类成员
- 由派生类设置的 constexpr 基类成员
- 从派生类调用的抽象基类成员函数
- 从抽象基类访问另一个基类成员
- C++派生类访问基类成员
- 不能引用派生模板类中的基类成员
- C2694 在析构函数上,当基类成员的析构函数具有非空 noexcept 说明符和主体时
- 在Visual Studio 2017中,通过扩展每个参数包来呼叫基类成员失败
- 在C 中访问带有不正确的下属的基类成员
- C++将基类成员链接到派生类成员
- 从基类成员函数返回派生类的实例
- 从指针到基类成员的模板推导
- 是否可以从派生类中排除基类成员?