简体   繁体   English

对于这个朋友重载运算符 function 而不仅仅是 std::cout,std::ostream& out 的目的是什么?

[英]What is the purpose of std::ostream& out for this friend overloaded operator function and not just std::cout?

Can someone explain the purpose of std::ostream &out for this friend overloaded operator function?有人可以为这个朋友重载运算符 function 解释std::ostream &out的目的吗? Why not just use std::cout inside the function?为什么不在 function 中使用std::cout

friend std::ostream& operator<<(std::ostream& out, const Complex& c) 
{
    out << c.real;
    out << "+i" << c.imag << endl;

    return out;
}

Instead, like so:相反,像这样:

friend operator<<(const Complex& c) 
{ 
    std::cout << c.real;
    std::cout << "+i" << c.imag << endl;
}

Because this way you can use any ostream as output, not just std::cout for example, std::clog << c;因为这样你可以使用任何ostream作为 output,而不仅仅是std::cout例如std::clog << c;

Displaying the output in a function and returning the output stream to the overloaded friend function have different meanings. Displaying the output in a function and returning the output stream to the overloaded friend function have different meanings.

You can pass the initialized std::ostream& to any other ostream& .您可以将初始化的std::ostream&传递给任何其他ostream& For example, std::cout , std::clog , std::cerr .例如, std::coutstd::clogstd::cerr

Note that this can be passed to the file stream instances such as std::ofstream when writing to a file.请注意,这可以在写入文件时传递给文件 stream 实例,例如std::ofstream 1 1

Also, in your second given example:另外,在您给出的第二个示例中:

friend operator<<(const Complex &c)

You are missing a return type.您缺少返回类型。 The program will fail to compile.该程序将无法编译。 Also, two arguments are required to overload the operator<< .此外,需要两个 arguments 来重载operator<<


1. Thanks to @Pete Becker for suggesting this important information. 1. 感谢@Pete Becker提供这些重要信息。

This:这个:

 friend std::ostream & operator << (std::ostream &out, const Complex &c) { out << c.real; out << "+i" << c.imag << endl; return out; }

is the function that gets called when you write:是在您编写时调用的 function:

Complex c;
std::cout << c;    // same as: operator<<( std::cout, c);

Calling std::cout << c;调用std::cout << c; inside the implementation of operator<< would be bad, because then it would call itself recursively with no end.operator<<的实现内部会很糟糕,因为它会递归地调用自己而没有结束。

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

相关问题 “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? 重载运算符std :: ostream&operator &lt;&lt;打印实例内存地址 - Overloaded operator std::ostream& operator<< prints instance memory address 如何解决这些错误? “&#39;template std :: ostream&operator &lt;&lt;的重新定义”,“未解析的重载函数类型” - How can I fix these errors? “redefinition of 'template std::ostream& operator<<” , “unresolved overloaded function type” 无法将std :: cout传递给ostream&构造函数 - Cannot pass std::cout to ostream& constructor &#39;运算符&lt;&lt;(std :: ostream&,int&)&#39;不明确 - 'operator<<(std::ostream&, int&)’ is ambiguous std :: ostream&运算符&lt;&lt;类型推导 - std::ostream& operator<< type deduction 朋友 std::ostream&amp; operator&lt;&lt; 声明不允许我访问类的私有成员 - friend std::ostream& operator<< declaration doesn't let me access the class' private members -&gt; std :: ostream&是什么意思? - What does ->std::ostream& mean? 命名空间+重载std :: ostream &lt;&lt;运算符 - Namespace + overloaded std::ostream << operator std :: ostream&运算符&lt;&lt;(std :: ostream&sstr,const T&val)的模棱两可的重载 - Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM