加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在C中覆盖虚函数

发布时间:2020-12-15 23:26:05 所属栏目:百科 来源:网络整理
导读:我有一个带虚函数的基类 – int Start(bool) 在派生中,有一个具有相同名称但具有不同签名的函数 – int Start(bool,MyType *) 但不虚拟 在派生的Start()中,我想调用基类Start() int Derived::Start(bool b,MyType *mType){ m_mType = mType; return Start(b)
我有一个带虚函数的基类 – 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#中,同样的情景也适用.

据我所知,虚拟机制不能用于解析调用,因为这两个函数具有不同的签名.

但这些电话没有按预期得到解决.

请帮忙

解决方法

您的两个选项是使用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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读