C++回调函数 与 C# delegate
2016-05-28 10:42
417 查看
mypersion.h
mypersion.cpp
main.cpp
![](http://img.blog.csdn.net/20160528101839673?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
C# Delegate
mypersion.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cscallback
{
class Program
{
static void Main(string[] args)
{
mypersion p = new mypersion(1, "涵", 1);
mypersion.CallBack(p.setter,1,"x",1);
while(true);
}
}
}
#ifndef MY_PERSION_H_ #define MY_PERSION_H_ #include<string> #include<iostream> using namespace std; class mypersion{ private: int age; string name; int sex; public : mypersion(int age, string name, int sex); //|第一个参数就是函数指针需要代入 返回值为void 函数签名为int ,string,int的函数 void callback(void (*callback)(int, string, int),int age,string name,int sex ); }; void setter(int age, string name, int sex); #endif
mypersion.cpp
#include"mypersion.h" mypersion::mypersion(int age, string name, int sex) { this->age = age; this->name = name; this->sex = sex; } void mypersion::callback(void(*callback)(int, string, int), int age, string name, int sex) { (*callback)(age, name, sex); cout << "您更改了成员属性" << endl; } void setter(int age, string name, int sex) { cout << "有值改变了" << endl; }
main.cpp
#include<iostream> #include"mypersion.h" using namespace std; int main(int argc, char * argv[]) { mypersion * p = new mypersion(19, "涵涵", 1); p->callback(setter, 1, "涵涵", 1); cout << "hello world" << endl; while (1); }
C# Delegate
mypersion.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cscallback { class mypersion { private int age; string name; int sex; public mypersion(int age, string name, int sex) { this.age = age; this.name = name; this.sex = sex; } public void setter(int age,string name,int sex) { Console.WriteLine("调用了setter"); } public delegate void Func(int a,string n,int s); public static void CallBack(Func fun,int age,string name,int sex) { fun(age, name, sex); } } }program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cscallback
{
class Program
{
static void Main(string[] args)
{
mypersion p = new mypersion(1, "涵", 1);
mypersion.CallBack(p.setter,1,"x",1);
while(true);
}
}
}
相关文章推荐
- 纯C语言矩阵乘法的Strassen算法,包含非2次幂的情况
- vector iterator not incrementable错误解决
- c++标准化前后
- YOJ 2335-0-1背包问题(输出最优装入方案)
- 【C++】const关键字用法总结
- 【C++】static关键字用法总结
- POJ 2955-Brackets(括号匹配-区间DP)
- 【C++】heap corruption detected错误调试总结及拓展
- c++第6次实验
- c++第6次作业
- NYIST 737-石子合并(区间DP)
- C语言应用初步感受
- 滑雪与时间胶囊
- c语言中char的-128是怎么计算的
- leetcode #46 in cpp
- leetcode #45 in cpp
- C语言程序设计感想 电信15-2 张寻 150206220
- C++队列与优先队列
- C++ 句柄类
- C++ string 字符串 结尾 标志 \0