c ++ cli接口事件显式实现 [英] c++ cli interface event explicit implementation
问题描述
我试图将c#代码转换为c ++ / cli。一切顺利,直到我开始翻译界面事件显式实现为c ++ / cli语法。
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.
让我们说c#我有这个接口
Let's say in c# i have this interface
public interface Interface
{
public event MyEventHandler Event;
}
这是在Class中以显式方式实现的,因此不会与另一个成员的名字:
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;
}
我试图将Class转换成c ++ / cli如下:
I am trying to convert Class into c++/cli as follows:
public ref class Class : public Interface
{
virtual event MyEventHandler^ Event2 = Interface::Event
{
}
...
};
这不会编译给我在... = Interface :: Event部分的语法错误。有没有人知道什么是正确的语法,或者甚至存在于c ++ / cli?我花了一些时间在互联网上搜索,但没有碰到任何有用的。
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.
更新:这里是完整的c ++ / cli代码,演示了问题:
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;
};
VC ++ 2012输出的错误是error C2146:syntax error:missing;'before identifier' MyEventHandler'
The error output by VC++ 2012 is "error C2146: syntax error : missing ';' before identifier 'MyEventHandler'"
推荐答案
您必须使其看起来像这样:
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;
}
};
这篇关于c ++ cli接口事件显式实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!