⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex9_18.cpp

📁 Visual C++ 2005的源代码
💻 CPP
字号:
// Ex9_18.cpp : main project file.
// Using an unbound delegate

#include "stdafx.h"

using namespace System;

public ref class ThisClass
{
public:
  void Sum(int n)
  { Console::WriteLine(L"Sum result = {0} ", value+n); } 

  void Product(int n)
  { Console::WriteLine(L"product result = {0} ", value*n); } 

  ThisClass(double v) : value(v){}

private:
  double value;
}; 

public delegate void UBHandler(ThisClass^, int value);         

int main(array<System::String ^> ^args)
{
  array<ThisClass^>^ things = { gcnew ThisClass(5.0),gcnew ThisClass(10.0),
                                gcnew ThisClass(15.0),gcnew ThisClass(20.0),
                                gcnew ThisClass(25.0)
                              };
  
  UBHandler^ ubh = gcnew UBHandler(&ThisClass::Sum);  // Create a delegate object 

  // Call the delegate for each things array element
  for each(ThisClass^ thing in things)
    ubh(thing, 3);

   ubh += gcnew UBHandler(&ThisClass::Product);   // Add a function to the delegate

  // Call the new delegate for each things array element
  for each(ThisClass^ thing in things)
    ubh(thing, 2);
  
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -