C++流是如何工作的

How do C++ streams work?

本文关键字:工作 何工作 C++      更新时间:2023-10-16

我想知道流类在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),其中传入参数13将返回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;
}

运算符可以被视为具有语法糖的函数,允许使用干净的语法。这只是一个运算符重载的情况。