C++基础-结构体伪函数与多线程(void operator()(int))
2021/6/27 14:14:22
本文主要是介绍C++基础-结构体伪函数与多线程(void operator()(int)),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C++结构体的伪函数使用operator进行创建,使用void operator()(int) 来表示需要传入的参数
#include<iostream> using namespace std; struct func{ void operator()() //方法, 可以将对象名当做函数名使用 { cout << "hello china hello cpp" << endl; } void operator()(int i) { cout << "hello china hello cpp" << i << endl; } }; int main() { func f1; f1(); f1(1); func f2; f2(); cin.get(); }
伪函数与多线程,使用伪函数可以在创建的时候执行任务,个人理解,相当于是构造函数
#include <iostream> #include<thread> #include<windows.h> using namespace std; struct MyStruct { MyStruct() { cout << "create" << endl; } ~MyStruct() { cout << "endl" << endl; } void operator()() { MessageBoxA(0, "ABCDEFG", "123", 0); } }; int main() { MyStruct go1; thread t1(go1); MyStruct go2; thread t2(go1); // thread t3(MyStruct()); //不适合作为多线程参数, 因为销毁的太快了 // MyStruct *p = new MyStruct(); // int a(5); // int *p = new int(5); //()初始化, //MyStruct 构造函数, 构造一个临时对象, 匿名对象 MyStruct()(); cin.get(); }
这篇关于C++基础-结构体伪函数与多线程(void operator()(int))的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc