Delegate1.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:1k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- __delegate double NumericOp(double);
- __gc class Ops
- {
- public:
- static double square(double d)
- {
- return d*d;
- }
- static double cube(double d)
- {
- return d*d*d;
- }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- // Declare a delegate
- NumericOp* pOp = new NumericOp(0, &Ops::square);
- // Call the function through the delegate
- double result = pOp->Invoke(3.0);
- Console::WriteLine("Result is {0}", __box(result));
- // Declare a second delegate
- NumericOp* pOp2 = new NumericOp(0, &Ops::cube);
- // Call the function through the delegate
- double result2 = pOp2->Invoke(3.0);
- Console::WriteLine("Result of cube() is {0}", __box(result2));
- return 0;
- }