如何重载操作数<<像 ostream 样式一样使用
How to overload operands << to use like the ostream style
我正在使用线程,需要用互斥锁保护std::cout操作,但我不知道如何重载操作符<<按如下顺序使用:
myOut << "hello " << 55 << " world" << false << 45.4f << std::endl;
如果有人能帮我,我会谢谢你的。
谢谢大家,但我解决了这个问题,很容易但重复,我需要做运算符<<返回一个myclass,然后使用这个返回值调用运算符<<想写多少遍就写多少遍,像这样:
Log& operator<<(const std::string& p){
std::lock_guard<std::mutex> locker(mutex);
std::cout << p;
return *this;
}
Log& operator<<(const std::string& p){
std::lock_guard<std::mutex> locker(mutex);
std::cout << p.c_str();
return *this;
}
但是需要为你想使用的任何类型重载它们只需为它创建一个全局变量
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- 堆栈和队列是否像C++中的数组一样传递?
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 我们可以将集合的值存储在变量中吗?就像我们可以将数组的值存储在变量中一样
- 我如何在C++像在 Python 中一样实现 f 字符串?
- 自制的上衣:看起来一样,但不完全相同
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 像 CPP 中的控制台一样的应用程序
- 为什么C++中没有常量引用,就像常量指针一样?
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 有没有像给定的一样的 std::optional_function
- Java 是否像C++模板一样具有泛型推论?