c ++ cli接口事件显式实现

edw*_*123 4 events c++-cli explicit interface

我试图将c#代码转换为c ++/cli.一切顺利,直到我开始将接口事件显式实现转换为c ++/cli语法.

让我们说在c#我有这个界面

public interface Interface
{
    public event MyEventHandler Event;
}
Run Code Online (Sandbox Code Playgroud)

这是以明确的方式在Class中实现的,因此它不会通过其名称与另一个成员冲突:

public interface Class : Interface
{
    event MyEventHandler Interface.Event;

    public event AnotherEventHandler Event;
}
Run Code Online (Sandbox Code Playgroud)

我试图将Class转换为c ++/cli,如下所示:

public ref class Class : public Interface
{
    virtual event MyEventHandler^ Event2 = Interface::Event
    {
    }

    ...
};
Run Code Online (Sandbox Code Playgroud)

这将无法编译,在"... = Interface :: Event"部分给出了语法错误.有没有人知道什么是正确的语法,或者它甚至存在于c ++/cli中?我花了一些时间在互联网上搜索,但未能碰到任何有用的东西.

更新:这是完整的c ++/cli代码,用于演示此问题:

public delegate void MyEventHandle();
public delegate void AnotherEventHandle();

public interface class Interface
{
    event MyEventHandler^ Event;
};

public ref class Class : public Interface
{
public:
    virtual event MyEventHandler^ Event2 = Interface::Event
    {
        virtual void add(MyEventHandle^) {}
        virtual void remove(MyEventHandle^) {}
    }

    event AnotherEventHandler^ Event;
};
Run Code Online (Sandbox Code Playgroud)

VC++ 2012输出的错误是"错误C2146:语法错误:缺少';' 在标识符'MyEventHandler'之前

Han*_*ant 5

你必须让它看起来像这样:

event MyEventHandler^ Event2 {
    virtual void add(MyEventHandler^ handler) = Interface::Event::add {
        backingDelegate += handler;
    }
    virtual void remove(MyEventHandler^ handler) = Interface::Event::remove {
        backingDelegate -= handler;
    }
};
Run Code Online (Sandbox Code Playgroud)