c++ cli interface event explicit implementation

霸气de小男生 提交于 2019-12-12 12:12:41

问题


I am trying to convert c# code into c++/cli. Everything went smoothly until i started translating interface event explicit implementations into c++/cli syntax.

Let's say in c# i have this interface

public interface Interface
{
    public event MyEventHandler Event;
}

Which is implemented in Class in explicit way, so it doesn't conflict with another member by its name:

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

    public event AnotherEventHandler Event;
}

I am trying to convert Class into c++/cli as follows:

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

    ...
};

This won't compile giving me syntax error in "... = Interface::Event" part. Does anyone have idea what is the right syntax, or does it even exist in c++/cli? I spent some time searching over the Internet, but failed to bump into anything useful.

UPDATE: Here is complete c++/cli code that demonstrates the problem:

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;
};

The error output by VC++ 2012 is "error C2146: syntax error : missing ';' before identifier 'MyEventHandler'"


回答1:


You have to make it look like this:

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


来源:https://stackoverflow.com/questions/15404891/c-cli-interface-event-explicit-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!