Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c+中的代表+; 我想在C++中实现委托我知道如何在C++中发布我的代码但不知道如何把这个转换成C++ < /p> public class Events { public delegate void Action(); public Action OnPrintTheText = delegate{}; } public class ABC { private Event evt; public ABC() { evt = new Event(); } public void printText() { evt.OnPrintTheText(); } public Event getEventHandler() { return evt; } } public class Drived { private ABC abc; public Drived() { abc = new ABC(); abc.getEventHandle().OnPrintTheText += OnPrint; } ~Drived() { abc.getEventHandle().OnPrintTheText -= OnPrint; } public void OnPrint() { Debug.Log("ABC"); } }_C#_C++_Delegates - Fatal编程技术网

C# c+中的代表+; 我想在C++中实现委托我知道如何在C++中发布我的代码但不知道如何把这个转换成C++ < /p> public class Events { public delegate void Action(); public Action OnPrintTheText = delegate{}; } public class ABC { private Event evt; public ABC() { evt = new Event(); } public void printText() { evt.OnPrintTheText(); } public Event getEventHandler() { return evt; } } public class Drived { private ABC abc; public Drived() { abc = new ABC(); abc.getEventHandle().OnPrintTheText += OnPrint; } ~Drived() { abc.getEventHandle().OnPrintTheText -= OnPrint; } public void OnPrint() { Debug.Log("ABC"); } }

C# c+中的代表+; 我想在C++中实现委托我知道如何在C++中发布我的代码但不知道如何把这个转换成C++ < /p> public class Events { public delegate void Action(); public Action OnPrintTheText = delegate{}; } public class ABC { private Event evt; public ABC() { evt = new Event(); } public void printText() { evt.OnPrintTheText(); } public Event getEventHandler() { return evt; } } public class Drived { private ABC abc; public Drived() { abc = new ABC(); abc.getEventHandle().OnPrintTheText += OnPrint; } ~Drived() { abc.getEventHandle().OnPrintTheText -= OnPrint; } public void OnPrint() { Debug.Log("ABC"); } },c#,c++,delegates,C#,C++,Delegates,现在每当我调用PrtTeXT时,它会自动调用驱动类的OnPrimTo(),所以无论如何都要实现这一点,以C++(?)/P> < P>查看函数指针: 函数指针是指向特定函数的指针,基本上就是委托 看看这个: C#在幕后进行大量管理。广义地说,C#中的委托是函数引用的容器 在C++中,可以使用一个函数模板来创建类似的委托,该模板覆盖一个对象实例和一个成员函数,用于一个类成员函数,另一个函数只封装一个函数(非成员)。然后,您可以使用标准容器来维护functor实例的列表/数组/映射/等,这将为您提供

现在每当我调用PrtTeXT时,它会自动调用驱动类的OnPrimTo(),所以无论如何都要实现这一点,以C++(?)/P> < P>查看函数指针:

函数指针是指向特定函数的指针,基本上就是委托

看看这个: C#在幕后进行大量管理。广义地说,C#中的委托是函数引用的容器

在C++中,可以使用一个函数模板来创建类似的委托,该模板覆盖一个对象实例和一个成员函数,用于一个类成员函数,另一个函数只封装一个函数(非成员)。然后,您可以使用标准容器来维护functor实例的列表/数组/映射/等,这将为您提供C#委托的功能,并允许添加和删除“操作”(C#::+=和-=)


请看我关于如何为类成员创建这样一个模板函子的回答。非成员情况更简单,因为它不包装对象实例

看看纯虚函数和抽象类。委托只不过是一个退化的源/接收器接口声明,其中包含一个特定函数。函数指针也很相似。你是想在C++/CLI中实现这一点吗?这是一篇关于C++代表的文章:一个例子和解释在这里非常有用。因此,仅仅指向链接是不可接受的。