有一个班级和一个代表C#
public delegate void Super();
public class Event
{
public event Super activate ;
public void act()
{
if (activate != null) activate();
}
}
和C ++ / Cli
public delegate void Super();
public ref class Event
{
public:
event Super ^activate;
void act()
{
activate();
}
};
在C#中我在类中创建多播委托(方法Setplus和setminus)
public class ContainerEvents
{
private Event obj;
public ContainerEvents()
{
obj = new Event();
}
public Super Setplus
{
set { obj.activate += value; }
}
public Super Setminus
{
set { obj.activate -= value; }
}
public void Run()
{
obj.act();
}
}
但是在C ++ / Cli中我遇到了错误 - usage requires Event::activate to be a data member
public ref class ContainerEvents
{
Event ^obj;
public:
ContainerEvents()
{
obj = gcnew Event();
}
property Super^ Setplus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Combine(obj->activate,value));
}
}
property Super^ SetMinus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Remove(obj->activate,value));
}
}
void Run()
{
obj->act();
}
};
问题出在哪里?
答案 0 :(得分:2)
请参阅:http://msdn.microsoft.com/en-us/library/ms235237(v=vs.80).aspx
C ++ / CLI遵循与C#相同的模拟。在C#中定义它是违法的:
public Super Setplus
{
set { obj.activate = Delegate.Combine(obj.activate, value); }
}
C ++ / CLI也是如此。使用现代语法中定义的+ = / - =表示法。
property Super^ Setplus
{
void set(Super^ value)
{
obj->activate += value;
}
}