C++流是如何工作的?

7

我想了解C++中的流类是如何工作的。当你说:

cout<<"Hello\n";

“<<”在C++中是插入运算符,用于将数据插入到流中。cout是一个iostream对象,表示面向窄字符(char)的标准输出流。关于流类的详细解释和实现方式,请提供更多信息。

1
请参阅 http://www.cplusplus.com/doc/tutorial/basic_io/ 和 http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/。 - Robert Harvey
3
C++支持运算符重载。不要将其与在不同上下文中用于整数等对象的相似运算符(ostream.<<)混淆。也就是说,运算符只是提供了一种更简洁的方式来编写“普通”方法名,例如 cout.insertFormattedOutout("Hello\n") - user2864740
"<<" 是一个操作符,类似于 "+"。可以将其理解为将字符串 "Hello\n" 推入对象 cout 中。也可以定义另一个像 "+" 一样的操作符来完成相同的操作。 - imran
详细解释?嗯......http://www.cprogramming.com/tutorial/c++-iostreams.html - 101010
3个回答

13

让我们创建一个看起来像 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 呢? - ajaysinghnegi
os_t不是ostream的子类。这里的问题基本上是如何实现流。 - Bill Lynch
实际上,我在谈论同一个(ostream)。 - ajaysinghnegi

5

<<是C++中的二元运算符,因此可以进行重载。

您知道这个运算符在C语言中的用法,其中1 << 3是返回8的二进制操作。可以将其视为方法int operator<<(int, int),其中传入参数13返回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;
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接