void operator()()的功能
在学习多线程的时候看到这样的一段代码,为什么要重载()呢?真有这个必要吗?
#include <iostream>
#include <thread>
class Counter {
public:
Counter(int value) : value_(value) {
}
void operator()() {
while (value_ > 0) {
std::cout << value_ << " ";
--value_;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << std::endl;
}
private:
int value_;
};
int main() {
std::thread t1(Counter(3));
t1.join();
std::thread t2(Counter(3));
t2.detach();
// 等待几秒,不然 t2 根本没机会执行。
std::this_thread::sleep_for(std::chrono::seconds(4));
return 0;
}
对 void operator()()的功能表示困惑
// 查阅了一些资料,这个是简易的说明代码
class background_task
{
public:
void operator()() const
{
do_something();
do_something_else();
}
};
background_task f;
std::thread my_thread(f);
在这里,为什么我们需要operator()()?第一个和第二个()的意思是什么?其实我知道这是重载运算符的操作()
我们在学习C++的时候,学习过运算符重载。这里重载() 可以使对象像函数那样使用,常常被称为函数对象。 (这里用作线程对象的构造)
第一个()是运算符的名称 – 它是在对象上使用()时调用的运算符. 第二个()是用于参数的。
以下是您如何使用它的示例:
background_task task;
task(); // calls background_task::operator()
有参数的演示:
#include <iostream>
class Test {
public:
void operator()(int a, int b) {
std::cout << a + b << std::endl;
}
};
int main() {
Test t;
t(3,5);
return 0;
}
// 输出结果
// 8
void operator()()的功能的更多相关文章
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- 浅谈new operator、operator new和placement new 分类: C/C++ 2015-05-05 00:19 41人阅读 评论(0) 收藏
浅谈new operator.operator new和placement new C++中使用new来产生一个存在于heap(堆)上对象时,实际上是调用了operator new函数和placeme ...
- 测试functional的bind以及相关功能
注:在VS2010 UPDATE1下测试通过 /*测试functional的bind以及相关功能*/ #include <iostream> #include <functional ...
- Effective C++ 第二版 10) 写operator delete
条款10 写了operator new就要同时写operator delete 写operator new和operator delete是为了提高效率; default的operator new和o ...
- Effective C++ 第二版 8) 写operator new 和operator delete 9) 避免隐藏标准形式的new
条款8 写operator new 和operator delete 时要遵循常规 重写operator new时, 函数提供的行为要和系统缺省的operator new一致: 1)正确的返回值; 2 ...
- OpenSceneGraph几个重要功能节点练习
OpenSceneGraph几个重要功能节点练习 一. 空间变换节点 空间变换中最重要的是坐标系和矩阵运算了.OSG坐标系中使用右手系,Z轴垂直向上,X轴水平向右,Y轴垂直屏幕向里,与OpenGL和D ...
- C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)
下面来进行这段代码的分析: struct node { //定义一个结构体node(节点) int x; int y; int len; //node中有3个成员变量x,y,l ...
- c++中的new、operator new、placement new
一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...
- C++ 中 new 操作符内幕:new operator、operator new、placement new
一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...
随机推荐
- winxp系统连接服务器丢包解决方法
winxp系统连接服务器丢包解决方法 MFC编写一个打开网页的程序,发生异常没有获取到数据. 分析步骤: 1. 用getLastError()获取到的信息,(2)- 系统找不到指定的文件. 2. 用浏 ...
- [经验栈]C#监测IPv4v6网速及流量
1.前言 最近做项目需要用到监测网速及流量,我经过百度和墙内谷歌都没能快速发现监测IPV6流量和网速的用例:也经过自己的一番查询和调试,浪费了不少时间,现在作为经验分享出来希望大家指正. 2.C# ...
- express 框架的使用方法
express 框架的使用方法: 第一步: 生成一个 (express)项目工程 命令提示框的指令是: express (文件名) express -e (文件名) 两段指令的 ...
- SpringBoot中注入ApplicationContext对象的三种方式
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 在项目中,我们可 ...
- 解决React Native安装应用到真机(红米3S)报Execution failed for task ':app:installDebug'的错误
报错信息如下: :app:installDebug Installing APK 'app-debug.apk' on 'Redmi 3S - 6.0.1'Unable to install D:\R ...
- 尚学堂 217 java中的字节码操作2
package com.bjsxt.test; @Author(name="gaoqi", year=2014) public class Emp { private int em ...
- Spring:一、基本模块思维导图
- springboot自动装配原理
最近开始学习spring源码,看各种文章的时候看到了springboot自动装配实现原理.用自己的话简单概括下. 首先打开一个基本的springboot项目,点进去@SpringBootApplica ...
- IOT设备SmartConfig实现
一般情况下,IOT设备(针对wifi设备)在智能化过程中需要连接到家庭路由.但在此之前,需要将wifi信息(通常是ssid和password,即名字和密码)发给设备,这一步骤被称为配网.移动设备如An ...
- Github中添加SSH key
1-创建密钥,在终端输入下面的命令 ssh-keygen -t rsa -b -C "你的邮箱" //双引号不能去 要求输入密码,建议回车使用空密码方便以后的每次连接,此时会生成一 ...