我想了解C++中的流类是如何工作的。当你说:
cout<<"Hello\n";
“<<”在C++中是插入运算符,用于将数据插入到流中。cout是一个iostream对象,表示面向窄字符(char)的标准输出流。关于流类的详细解释和实现方式,请提供更多信息。
让我们创建一个看起来像 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 << "hello\n";
os << "chaining " << "works too." << "\n";
}
注意:
operator<<
就像operator+
和所有其他运算符一样,是一种操作符重载。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;
}
int
作为第二个参数的自由函数,而 ostream
类也重载了 << 运算符
用于 int 呢? - ajaysinghnegios_t
不是ostream
的子类。这里的问题基本上是如何实现流。 - Bill Lynch<<
是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" << "!";
,它在std::cout和“Hello World”上调用了一次operator<<
,然后在第一次调用的结果和“!”上调用了第二次。
通常,如果您要创建一个名为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;
}
ostream.<<
)混淆。也就是说,运算符只是提供了一种更简洁的方式来编写“普通”方法名,例如cout.insertFormattedOutout("Hello\n")
。 - user2864740