简体   繁体   English

为什么这个重载的 operator<< 以意想不到的方式工作?

[英]Why does this overloaded operator<< work in an unexpected way?

I wanted to create a method to add color to console output that would work in a similar way to std::left and std::setw() .我想创建一种向控制台 output 添加颜色的方法,其工作方式与std::leftstd::setw()类似。 I ended up with the code below, and it works exactly how I want it to.我最终得到了下面的代码,它完全按照我想要的方式工作。

I understand how it works, but I would like some clarifications on some things to better my knowledge.我了解它是如何工作的,但我想对某些事情进行一些澄清以加深我的知识。

Here is the code:这是代码:

#include <iostream>
#include <Windows.h>

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

enum class color { blue = FOREGROUND_BLUE, green, cyan, red, purple, yellow, white, bright = FOREGROUND_INTENSITY };

class coutColor {
public:

    WORD Color;

    coutColor(color colorvalue) : Color((WORD)colorvalue) {  }
    ~coutColor() { SetConsoleTextAttribute(hConsole, (WORD)7); }

};

std::ostream& operator<<(std::ostream& os, const coutColor& colorout) {

    SetConsoleTextAttribute(hConsole, colorout.Color);
    return os;

}

int main() {

    std::cout << coutColor(color::green) << "This text is green!\n";
    std::cout << color::red << "This text is red! " << 31 << "\n";

    return 0;
}

I understand how coutColor(color::green) works in the cout in main() , but why does just color::red by itself work as well?我了解coutColor(color::green)如何在main()cout中工作,但为什么color::red本身也能工作?

I stumbled upon it by accident while testing different things.我在测试不同的东西时偶然发现了它。

How can it take the enum type color as an input, since it's not in the input parameters of the overloaded operator<< ?它如何将enum类型color作为输入,因为它不在重载operator<<的输入参数中?

Why does it do the same thing as inputting coutColor(color::red) ?为什么它与输入coutColor(color::red)做同样的事情?

why does just color::red by itself work as well?为什么color::red本身也可以工作? ... How can it take the enum type color as an input, since it's not in the input parameters of the overloaded operator<< ? ...如何将enum类型color作为输入,因为它不在重载operator<<的输入参数中? Why does it do the same thing as inputting coutColor(color::red) ?为什么它与输入coutColor(color::red)做同样的事情?

It is because coutColor 's constructor is not marked as explicit .这是因为coutColor的构造函数未标记为explicit

When the compiler is looking for a suitable overload of operator<< for the expression std::cout << color::red , it finds your overload in scope and sees that:当编译器正在为表达式std::cout << color::red寻找合适的operator<<重载时,它会在 scope 中找到您的重载并看到:

  1. coutColor is implicitly constructable from a color value coutColor可从color隐式构造

  2. the operator takes a coutColor object by const reference操作员通过const 引用获取coutColor object

So, the compiler is able to create a temporary coutColor object, passing the color value to its constructor, and then pass that object to the operator.因此,编译器能够创建一个临时coutColor object,将color值传递给它的构造函数,然后将该 object 传递给运算符。

I have seen function(input): var(input) {} before and I didn't know what it meant.之前看过function(input): var(input) {}也不知道是什么意思。

C++, What does the colon after a constructor mean? C++,构造函数后面的冒号是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM