CLI/C 中绑定和未绑定委托之间的差异++

发布于 2024-12-03 17:00:32 字数 1421 浏览 0 评论 0原文

绑定委托和非绑定委托有什么区别?

以下是创建两种类型委托的方法:

// bound delegate declaration
public delegate void Handler(int value);
// create a bound delegate
Handler^ handler = gcnew Handler(HandlerClass::Fun1);

// unbound delegate declaration
public delegate void UBHandler(ThisClass^, int value); 
// create an unbound delegate
UBHandler^ ubh = gcnew UBHandler(&ThisClass::Sum); 

它们几乎相同。然后,您可以为包含两个参数的绑定委托创建构造函数:

HandlerClass^ obj = gcnew HandlerClass;
Handler^ handler2 = gcnew Handler (obj, & HandlerClass::Fun3);

这意味着您可以使用此特定委托来调用非静态(是实例)的函数。但是您可以对未绑定的委托执行相同的操作。以下是您如何调用 ubh 委托:

ThisClass^ obj = gcnew ThisClass(99.0);
ubh(obj, 5);

那么拥有这两种类型有什么意义呢?

// code for HandlerClass
public ref class HandlerClass
{
  public:

  static void Fun1(int m)
  { Console::WriteLine(L”Function1 called with value {0}”, m); }
  static void Fun2(int m)
  { Console::WriteLine(L”Function2 called with value {0}”, m); }
  void Fun3(int m)
  { Console::WriteLine(L”Function3 called with value {0}”, m+value); }
  void Fun4(int m)
  { Console::WriteLine(L”Function4 called with value {0}”, m+value); }

  HandlerClass():value(1){}
  HandlerClass(int m):value(m){}

  protected:

  int value;
};

Whats the difference between bound and unbound delegates?

heres how you create delegates of both types:

// bound delegate declaration
public delegate void Handler(int value);
// create a bound delegate
Handler^ handler = gcnew Handler(HandlerClass::Fun1);

// unbound delegate declaration
public delegate void UBHandler(ThisClass^, int value); 
// create an unbound delegate
UBHandler^ ubh = gcnew UBHandler(&ThisClass::Sum); 

these are nearly the same. then, you can create constructors for bound delegates that consist of two parameters:

HandlerClass^ obj = gcnew HandlerClass;
Handler^ handler2 = gcnew Handler (obj, & HandlerClass::Fun3);

it means that you can use this particular delegate to invoke a function that is not static (is an instance). but then you can do the same with unbound delegates. Here’s how you might call the ubh delegate:

ThisClass^ obj = gcnew ThisClass(99.0);
ubh(obj, 5);

so whats the point of having both types?

// code for HandlerClass
public ref class HandlerClass
{
  public:

  static void Fun1(int m)
  { Console::WriteLine(L”Function1 called with value {0}”, m); }
  static void Fun2(int m)
  { Console::WriteLine(L”Function2 called with value {0}”, m); }
  void Fun3(int m)
  { Console::WriteLine(L”Function3 called with value {0}”, m+value); }
  void Fun4(int m)
  { Console::WriteLine(L”Function4 called with value {0}”, m+value); }

  HandlerClass():value(1){}
  HandlerClass(int m):value(m){}

  protected:

  int value;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2024-12-10 17:00:32

差异在于生成目标对象值的确切时间。对于绑定委托,它是在创建委托对象时生成的,此后它永远不变。未绑定委托不存储对象引用,它是在调用委托时生成的。然后可以使用相同的委托来调用不同对象的目标方法。

未绑定委托与 C++ 成员函数指针的语法相匹配。 C++ 中的绑定委托没有直接等效的东西。

The difference is the exact time the target object value is generated. With a bound delegate it is generated when you create the delegate object and it is forever unchanging after that. An unbound delegate doesn't store the object reference, it is generated at the time the delegate is invoked. The same delegate can then be used to invoke the target method with different objects.

Unbound delegates are a match with the syntax of C++ member function pointers. There is no direct equivalent for bound delegates in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文