如何使用C++/CLI调用重写的基类方法

Joh*_*son 3 .net c++-cli

实现此C#代码的正确方法是什么:

protected override void SomeMethod(inputs)  
{  
    ... do stuff ..  
    base.SomeMethod(inputs);  
}  
Run Code Online (Sandbox Code Playgroud)

在C++/CLI中

Alo*_*ave 8

通过使用基类名称定义方法名称.

void SomeMethod(inputs)
{
    ... do stuff ..
    base::SomeMethod(inputs);
}
Run Code Online (Sandbox Code Playgroud)

在线演示:

#include<iostream>
class Base
{
public:
    virtual void doSomething()
    {
        std::cout<<"In Base";
    }
};

class Derived:public Base
{
public:
    virtual void doSomething()
    {
        std::cout<<"In Derived";
        Base::doSomething();
    }
};

int main()
{
    Base *ptr = new Derived;
    ptr->doSomething();
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

  • 注意:提问者告诉`base.SomeMethod(inputs);`其中`base`是C#关键字而不是基类的名称. (2认同)