简体   繁体   English

“基本C ++”:提供iostream运算符的类实例

[英]“Essential C++”: Providing Class Instances of the iostream Operators

From Essential C++: 4.10 Providing Class Instances of the iostream Operators Essential C ++: 4.10提供iostream运算符的类实例

Often, we wish to both read and write objects of a class. 通常,我们希望同时读取和写入类的对象。 For example, to display our trian class object, we want to be able to write 例如,要显示我们的trian类对象,我们希望能够编写

cout << train << endl;

To support this, we must provide an overloaded instance of the output operator: 为此,我们必须提供输出运算符的重载实例:

ostream& operator<< (ostream &os, const Triangular &rhs)
{
    os << "(" << rhs.beg_pos() << "," << rhs.length() << ")";
    rhs.display(rhs.length(), rhs.beg_pos(), os);
    return os;
}

We return the same ostream object passed into the function. 我们返回传递给函数的同一个ostream对象。 This allows multiple outptu operators to be concatenated. 这允许将多个输出运算符连接在一起。 Both objects are passed by reference. 这两个对象都通过引用传递。 The ostream operand is not declared as const because each output operation modifies the internal state of the ostream object. ostream操作数未声明为const,因为每个输出操作都会修改ostream对象的内部状态。

I'm kind of confused why the ostream operand cannot be declared as const. 我有点困惑为什么不能将ostream操作数声明为const。 If the output operator is declared as the following: 如果输出运算符声明为以下内容:

const ostream& operator<< (const ostream &os, const Triangular &rhs)

Is there any problem with the above declaration? 上面的声明有什么问题吗?

Thanks 谢谢

The problem is that if the ostream argument (or conversely istream ) was a constant reference, then the operator would not be able to modify the stream object. 问题在于,如果ostream参数(或相反, istream )是一个常量引用,那么操作员将无法修改流对象。 Insertion/extraction to the streams modify the stream state, so the existing operator<< are non-const operations. 流的插入/提取会修改流状态,因此现有的operator<<是非常量操作。 That in turn means that while you can declare and even define : 这反过来又意味着,虽然你可以声明 ,甚至定义

std::ostream const & operator<<( std::ostream const & s, Type const & t );

The problem is that the definition would not be able to actually write anything at all into the stream: 问题是该定义实际上无法将任何内容实际写入流中:

std::ostream const & operator<<( std::ostream const & s, Type const & t ) {
   s << "Hi";     // Error: operator<<( std::ostream&, const char*) requires a
                  //        non-const `std::ostream&`
   return s;      // This is fine
}

When outputting the variable rhs , some data members inside ostream& os such as output buffer or position of file write if os is a ofstream must be modified. 当输出变量rhs ,必须修改ostream& os内部的某些数据成员ostream& os例如osofstream输出缓冲区或文件写入位置。

Declaring os as const forbids such a modification. os声明为const禁止进行此类修改。

And, as shown here , if os is declared as const , then you cannot use it to output primitive data types since none of ostream::operator<<() is declared as constant member function. 而且,如图所示这里 ,如果os被声明为const ,那么你不能因为没有使用的输出基本数据类型ostream::operator<<()被声明为常量成员函数。

是的,可以通过调用<<来修改ostream参数os。

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

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