本文主要是介绍C++之callbcak左值右值引用(九十九),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.代码示例
#include <iostream>
#include <string>
#include <pthread.h>
#include <functional>
using namespace std;
typedef void (*MyFun)(int n, string str);
typedef std::function <void(int n, string str)> Func;static void Test(int n, string str){cout << "n = "<< n << " str = " << str <<endl;
}static void Test1(Func &cb){//左值引用cb(55555,"nnnnnn");
}static void Test2(Func &&cb, string buf){//右值引用cb(66666,buf);
}int main(){MyFun m1;Func cc;//callback 1m1 = Test;m1(111,"1233" );//callback 2auto func3 = std::bind(Test, std::placeholders::_1, std::placeholders::_2);func3(2222, "Hello");//callback 3cc = Test;cc(44444,"mmmmmm");Test1(cc);//左值引用//callback 4string buffer = "Hello callback";Test2(std::bind(Test, std::placeholders::_1, std::placeholders::_2), buffer);//右值引用string buf = "Hello callback 2222222";Test2(Test, buf);//右值引用return 0;}
这篇关于C++之callbcak左值右值引用(九十九)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!