单例宏及函数包装器function的使用

1)单例模式中,用单例宏定义,测试代码如下:

#include <iostream>
using namespace std;

typedef struct 
{
        int a;
}ABChassie_info_t;

#define DECLARE_SINGLETON_WITHOUT_CONSTRUCTOR(A) \
public: \
        static A* Instance(void)     \
        {       \
                static A instance;      \
                return &instance;       \
        }       \

class BB
{
public:
		//其实就是定义一个成员函数 ,ABChassie_info_t 就是一个类型而已。
		//定义一个函数,函数的返回值是类,想当于声明一个:int func();
        ABChassie_info_t cscan_canbus_info(); //声明一个成员函数。
        DECLARE_SINGLETON_WITHOUT_CONSTRUCTOR(BB);
};

ABChassie_info_t BB::cscan_canbus_info()
{
        cout<<"hello world"<<endl;
        ABChassie_info_t num;
        num.a = 10;
        return  num;
}

int main()
{
        BB* bptr = new BB();
        auto ret = bptr->Instance()->cscan_canbus_info();
        cout<<"ret.a is:  "<<ret.a<<endl; //输出10
        return 0;
}

2)std::bind函数适配器及std::function函数包装器的配合使用,测试代码如下:
利用了回调函数的使用方法:

#include <iostream>
#include<functional>
#include<thread>

using namespace std;

typedef std::function<int(const std::string &, const char *, const int)>
    Callback; //函数包装器

class DataManagement
{
public:
       int data_process_callback(const std::string & name, const char *data, const int len);
};

int DataManagement::data_process_callback(const std::string & name, const char *data, const int len)
{
        return 20;
}

class readMsg
{
 public:
  explicit readMsg(Callback callback){}
  ~readMsg() {  }
  int test(void) { return 30; } 
};

int main()
{
        DataManagement datManger;
        using namespace std::placeholders;
        //注意:int(const std::string &, const char *, const int)对应的就是“_1, _2, _3”;
        Callback cb1 = std::bind(&DataManagement::data_process_callback,&datManger, _1, _2, _3);
        readMsg rd(cb1);
        while(1)
        {
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<rd.test()<<endl; //间隔1秒输出30;
        }
        return 0;
}

3)终于明白了std::function的设计为回调函数的使用方法,测试代码如下:

#include <iostream>
#include <functional>
#include <string>
using namespace std;

//typedef std::function<void(int)> callBack_t; //typedef可以
using callBack_t = std::function<void(int)>; //using也可以

void func1(callBack_t f)
{
    f(10); //最终调用了test函数
}

void test(int num) //test作为回调函数
{
    cout << "num: " << num << endl;
}
//回调函数就是把函数地址提供给调用者!!!没有什么难的;
int main()
{
    func1(test); //把test的函数地址(函数指针)给func1,
    return 0;
}

4)再次理解std::function和std::bind绑定器的结合使用,(自己写的测试代码)

#include<iostream>
#include<functional>

using namespace std;

typedef std::function<void(int,int,int)> callBack_t;

void func(callBack_t f)
{
	f(1,2,3);
}

void test(int x = 10,int y = 20,int z = 30)
{
	cout << "x: " << x << endl;
	cout << "y: " << y << endl;
	cout << "z: " << z << endl;
}

int main(void)
{
	func(test); //输出1,2,3
	callBack_t cb = std::bind(test,100, 200, std::placeholders::_3);
	func(cb); //输出100,200,3
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值