visual-c++ - 为什么以下代码不会覆盖 C++ CLI 中的基类属性?

标签 visual-c++ visual-studio-2012 c++-cli overriding

我是一名 C# 程序员,对 C++ 不太了解。 知道为什么我会收到错误吗?

ref class masterWeapon{
public :
    virtual property int Slot {
        int get(){
            return -1;
        }
    }
};

ref class Weapon1 : masterWeapon{

public :
    virtual property int Slot{
//following like throw an error :  cannot override base class method 
        int get() override = masterWeapon::Slot::get{
            return 1;
        }
    }

};

最佳答案

只需删除 = masterWeapon::Slot::get 部分即可编译。如果您阅读 C3764 附带的错误消息,它会让这一点变得更加明显(但不是 100%):

...because the base method is explicitly overridden by 'Weapon1::Slot::get'

给我们以下代码:

ref class Weapon1 : masterWeapon{
public :
    virtual property int Slot {
        int get() override {
            return 1;
        }
    }
};

当遇到:

masterWeapon^ weapon1 = gcnew masterWeapon();
masterWeapon^ weapon2 = gcnew Weapon1();

Console::WriteLine(L"weapon1->Slot = {0}", weapon1->Slot);
Console::WriteLine(L"weapon2->Slot = {0}", weapon2->Slot);

结果:

weapon1->Slot = -1

weapon2->Slot = 1

关于visual-c++ - 为什么以下代码不会覆盖 C++ CLI 中的基类属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15301725/

相关文章:

c# - 在没有设置项目的情况下安装 Windows 服务

c# - 在 Windows 8 应用程序中用手指触摸滚动列表框的问题

multithreading - 尝试多线程程序。 C++/CLI

c# - 使用 CLI 将 C# double 组传递给 C++ 函数

windows - Windows 10 上的 Visual Studio 2010 x64 编译器

c++ - 是否可以压缩 VC++ 运行时堆?

asp.net - 创建批处理文件以发布网站 ASP.net

windows - native x64 dll 不工作

C++:使用指向 unordered_map 的指针或只是将其定义为类中此类型的成员变量?

c# - 使用自动属性显式覆盖