c++ cli interface event explicit implementation

2019-06-28 05:21发布

问题:

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