在学习多线程的时候看到这样的一段代码,为什么要重载()呢?真有这个必要吗?

#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()()的功能的更多相关文章

  1. [c++] Operator overloading

    c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...

  2. 浅谈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 ...

  3. 测试functional的bind以及相关功能

    注:在VS2010 UPDATE1下测试通过 /*测试functional的bind以及相关功能*/ #include <iostream> #include <functional ...

  4. Effective C++ 第二版 10) 写operator delete

    条款10 写了operator new就要同时写operator delete 写operator new和operator delete是为了提高效率; default的operator new和o ...

  5. Effective C++ 第二版 8) 写operator new 和operator delete 9) 避免隐藏标准形式的new

    条款8 写operator new 和operator delete 时要遵循常规 重写operator new时, 函数提供的行为要和系统缺省的operator new一致: 1)正确的返回值; 2 ...

  6. OpenSceneGraph几个重要功能节点练习

    OpenSceneGraph几个重要功能节点练习 一. 空间变换节点 空间变换中最重要的是坐标系和矩阵运算了.OSG坐标系中使用右手系,Z轴垂直向上,X轴水平向右,Y轴垂直屏幕向里,与OpenGL和D ...

  7. C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)

    下面来进行这段代码的分析: struct node {  //定义一个结构体node(节点)    int x;    int y;    int len;   //node中有3个成员变量x,y,l ...

  8. c++中的new、operator new、placement new

    一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...

  9. C++ 中 new 操作符内幕:new operator、operator new、placement new

    一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...

随机推荐

  1. docker-mcr 助您全速下载 dotnet 镜像

    2018 年五月之后,微软将后续发布的所有 docker image 都推送到了 MCR (Miscrosoft Container Registry),但在中国大陆,它的速度实在是令人发指,本文将介 ...

  2. DES 加密解密 文件工具类

    public class DESEncrypt { /** 加密工具 */ private Cipher encryptCipher = null; /** 解密工具 */ private Ciphe ...

  3. Python 网络爬虫实战:爬取 B站《全职高手》20万条评论数据

    本周我们的目标是:B站(哔哩哔哩弹幕网 https://www.bilibili.com )视频评论数据. 我们都知道,B站有很多号称“镇站之宝”的视频,拥有着数量极其恐怖的评论和弹幕.所以这次我们的 ...

  4. springboot集成jpa操作mybatis数据库

    数据库如下 CREATE TABLE `jpa`.`Untitled` ( `cust_id` bigint() NOT NULL AUTO_INCREMENT, `cust_address` var ...

  5. VulnHub CengBox2靶机渗透

    ​本文首发于微信公众号:VulnHub CengBox2靶机渗透,未经授权,禁止转载. 难度评级:☆☆☆☆官网地址:https://download.vulnhub.com/cengbox/CengB ...

  6. Python自学——pygame安装

    本片文章介绍pygame的安装方法.一则跟广大初学者分享经历,二则做个自我总结. pygame是python的库文件,跟一般的应用软件安装方法不太一样.我电脑上的python版本是python3.7, ...

  7. 使用随机函数random来实现课堂点名

    如何使用函数random来实现课堂随机点名 1.最初的样子如下 2.点击开始点名,上面一行的文字变成名字,名字在不停的变化,开始点名变成停止点名,如下 3.点击停止点名,上面名字不动,停止点名变成开始 ...

  8. Mysql中交换行操作

    博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中交换行操作 leetcode的一道题目 参考:https://leetcode.com/proble ...

  9. Socket模拟Web服务器

    效果如下: 源码下载地址:https://github.com/doyoulaikeme/DotNetSample/tree/master/DotNetSample3/SocketWebServer

  10. Idea JAVA开发工具快速上手-常用快捷键汇总

    前言: 之前一直使用Eclipse 系列开发IDE工具,由于eclipse是开源的所以,一般情况,eclipse基本上每一个java入门者的首选开发工具,其次 Myeclipse.不过现在越来越多的人 ...