c# - C++中的虚函数重写

标签 c# c++ oop

我有一个带有虚函数的基类 - 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/

相关文章:

c# - C#字节加法溢出?

javascript - 尝试在 IHttpHandler 类中读取我的 http 调用的变量

c# - WPF:绑定(bind)后的窗口 SizeChanged 事件

c++ - 在 8 Puzzle | 中寻找曼哈顿距离的目标坐标C++

java - 根据传递的参数从树中获取选定的对象

c# - 我可以命名以数字开头的 C# namespace 吗?

c++ - listen() 是连续运行还是我需要循环它以继续接收套接字上的连接?

c++ - 当 operator() 调用时,返回 std::function 的 std::function 对象如何工作?

oop - 函数式编程是否被认为更多 "mathematical"?如果是这样,为什么?

matlab - 在构造函数帮助文件中显示用户创建的 MATLAB 对象的可用方法