它必须是我的代码中特定的东西,我无法发布.但也许有人可以提出可能的原因.
基本上我有:
class CParent
{
public:
void doIt(int x);
};
class CChild : public CParent
{
public:
void doIt(int x,int y,int z);
};
CChild *pChild = ...
pChild->doIt(123); //FAILS compiler, no method found
CParent *pParent = pChild;
pParent->doIt(123); //works fine
Run Code Online (Sandbox Code Playgroud)
怎么可能?
编辑:人们正在谈论阴影/隐藏.但是两个版本的doIt具有不同数量的参数.当然不能混淆编译器,子类中的重载哪些不可能与父类版本混淆?它可以?
我得到的编译器错误是: 错误C2660:'CChild :: doIt':函数不带1个参数
GMa*_*ckG 30
你已经遮蔽了一个方法.例如:
struct base
{
void method(int);
void method(float);
};
struct derived : base
{
void method(int);
// base::method(int) is not visible.
// base::method(float) is not visible.
};
Run Code Online (Sandbox Code Playgroud)
您可以使用using指令修复此问题:
class derived : public base
{
using base::method; // bring all of them in.
void method(int);
// base::method(int) is not visible.
// base::method(float) is visible.
};
Run Code Online (Sandbox Code Playgroud)
既然你似乎坚持参数的数量,我会解决这个问题.这不会改变任何事情.注意:
struct base
{
void method(int){}
};
struct derived : base
{
void method(int,int){}
// method(int) is not visible.
};
struct derived_fixed : base
{
using base::method;
void method(int,int){}
};
int main(void)
{
{
derived d;
d.method(1, 2); // will compile
d.method(3); // will NOT compile
}
{
derived_fixed d;
d.method(1, 2); // will compile
d.method(3); // will compile
}
}
Run Code Online (Sandbox Code Playgroud)
无论参数或返回类型如何,它仍将被遮蔽; 它只是阴影的名称.using base::<x>;将把所有的base的' <x>’方法引入的知名度.
| 归档时间: |
|
| 查看次数: |
3714 次 |
| 最近记录: |