C++流是如何工作的
How do C++ streams work?
我想知道流类在C++中是如何工作的。当你说:
cout<<"Hellon";
什么才是真正的"<<"。我知道cout是一个对象形式iostream,它表示面向窄字符(char)的标准输出流。
在C中,"<<"是按位移位运算符,因此它向左移动位,但在C++中,它是插入运算符。我只知道这些,我真的不明白这是怎么回事。
我想要的是关于C++中流类的详细解释,以及它们是如何定义和实现的。
非常感谢你抽出时间,也为我的英语感到抱歉。
让我们创建一个看起来像cout
的类(但没有那么多的铃铛和口哨)。
#include <string>
class os_t {
public:
os_t & operator<<(std::string const & s) {
printf("%s", s.c_str());
return *this;
}
};
int main() {
os_t os;
os << "hellon";
os << "chaining " << "works too." << "n";
}
注:
- CCD_ 2与
operator+
或所有其他运算符一样是运算符重载 - Chaining之所以有效,是因为我们回归了自己:
return *this;
如果因为别人写了os_t
类而不能更改它,该怎么办
我们不必使用成员函数来定义此功能。我们也可以使用免费功能。让我们也展示一下:
#include <string>
class os_t {
public:
os_t & operator<<(std::string const & s) {
printf("%s", s.c_str());
return *this;
}
};
os_t & operator<<(os_t & os, int x) {
printf("%d", x);
return os;
// We could also have used the class's functionality to do this:
// os << std::to_string(x);
// return os;
}
int main() {
os_t os;
os << "now we can also print integers: " << 3 << "n";
}
运算符重载还有什么用处
在GMP库中可以找到这种逻辑如何有用的一个很好的例子。这个库被设计为允许任意大的整数。我们通过使用自定义类来实现这一点。下面是它的使用示例。请注意,运算符重载让我们编写的代码看起来与使用传统int
类型的代码几乎相同。
#include <iostream>
#include <gmpxx.h>
int main() {
mpz_class x("7612058254738945");
mpz_class y("9263591128439081");
x = x + y * y;
y = x << 2;
std::cout << x + y << std::endl;
}
<<
是C++中的一个二进制运算符,因此可以重载。
您知道这个运算符的C用法,其中1 << 3
是返回8
的二进制运算。您可以将其视为方法int operator<<(int, int)
,其中传入参数1
和3
将返回8
。
从技术上讲,operator<<
可以做任何事情。这只是一个任意的方法调用。
按照C++中的惯例,<<
运算符除了作为比特移位运算符之外,还用于处理流。当您执行cout << "Hello!"
时,您正在调用一个具有原型ostream & operator<< (ostream & output, char const * stream_me)
的方法。注意返回值ostream &
。该返回允许您多次调用该方法,比如std::cout << "Hello World" << "!";
,它调用operator<<
0两次。。。一次是在std::cout和"Hello World"上,第二次是在第一次调用的结果和"!"上。
通常,如果要创建一个名为class Foo
的类,并且希望它是可打印的,则可以将打印方法定义为ostream & operator<< (ostream & output, Foo const & print_me)
。这里有一个简单的例子。
#include <iostream>
struct Circle {
float x, y;
float radius;
};
std::ostream & operator<< (std::ostream & output, Circle const & print_me) {
output << "A circle at (" << print_me.x << ", " << print_me.y << ") with radius " << print_me.radius << ".";
}
int main (void) {
Circle my_circle;
my_circle.x = 5;
my_circle.y = 10;
my_circle.radius = 20;
std::cout << my_circle << 'n';
return 0;
}
运算符可以被视为具有语法糖的函数,允许使用干净的语法。这只是一个运算符重载的情况。
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?
- sdl软件渲染器不工作,工作在硬件加速的一个