我有一个带有虚函数的基类 - int Start(bool)
在派生中有一个具有相同名称但具有不同签名的函数 -
int Start(bool, MyType *)
但不是虚拟的
在派生的Start()
中,我想调用基类Start()
int Derived::Start(bool b, MyType *mType)
{
m_mType = mType;
return Start(b);
}
但是它给出了编译错误。
"Start' : function does not take 1 arguments"
但是 Base::Start(b)
有效
在 C# 中,上述代码有效,即解析调用不需要引用 Base。
外部如果调用如下
Derived *d = new Derived();
bool b;
d->Start(b);
失败并显示消息:
Start : function does not take 1 arguments
但在 C# 中,同样的场景有效。
据我了解,虚拟机制不能用于解析调用,因为这两个函数具有不同的签名。
但调用没有按预期得到解决。
请帮忙
最佳答案
您的两个选项是添加 using Base::Start
来解析 Start
的范围
int Derived::Start(bool b, MyType *mType)
{
using Base::Start;
m_mType = mType;
return Start(b);
}
或者如您所述添加 Base::
前缀。
int Derived::Start(bool b, MyType *mType)
{
m_mType = mType;
return Base::Start(b);
}
关于c# - C++中的虚函数重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804980/