4

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'"

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
edwabr123
  • 97
  • 9
  • Normally when you got questions about error messages, it's a good idea to provide those messages in the question. – Arne Mertz Mar 14 '13 at 09:03
  • Don't have a compiler for testing here, but I would expect the line to read "virtual event EventHandle Interface::Event" without another name or assignment. – nvoigt Mar 14 '13 at 09:03
  • Updated post with with error message – edwabr123 Mar 14 '13 at 09:45
  • nvoigt, "virtual event EventHandle Interface::Event" is old c++/cli syntax and it wouldn't compile telling that there is an error before Event. The syntax, at least for explicit method declaration, is "Name = Scope::Name" – edwabr123 Mar 14 '13 at 10:23

1 Answers1

5

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;
    }
};
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536