为什么要将std :: ostream和朋友用于<<运算符重载?

时间:2017-10-17 15:15:00

标签: c++

我想重载运算符<<使用std :: cout打印列表,例如:

std::cout << list 
//ouputs as {1,2,3,..}

搜索之后,我开始知道这可以使用ostream来完成,但我不确定这是怎么做的。就像为什么这需要将ostream作为参考然后返回呢?

运算符重载功能:

ostream& operator<<(ostream& os, List list) 
{
    Node* temp = list.start; //points to the start of the list
    cout<<"{";
    while(temp)
    {
        cout << temp->data; //cout all the data
        if (temp->next != NULL)
            cout << ",";
        temp = temp->next;
    }
    cout<<"}" << endl;
    return os; //returns the ostream
}

而且我也不明白我们为什么要把它变成朋友的功能?如果我删除关键字friend,则会给我一个错误&lt;&lt; operator是二元运算符。我正在使用Xcode。

List.hpp

class List
{
    Node* start;
public:
    List();
    ~List();
    void emptyList();
    void append(int);
    void insert(int, int);
    void print();
    int  length();
    void remove_at(int);
    int get_value_index(int);
    int get_value_at(int);
    List operator-(int); 
    friend ostream& operator<<(ostream& os,List); //why friend function??
    List operator=(List);
    List operator+(int);
    List operator+(List);
    int& operator[](const int ind);
    bool operator==(List);
};

2 个答案:

答案 0 :(得分:3)

在运营商&lt;&lt;超载你不应该使用cout,你应该使用你收到的ostream作为参数,只需更改cout os。这是因为cout将打印您要返回的输出流。

您需要使该运算符重载友元函数,因为即使运算符不是类的一部分,它也可以访问类私有属性和函数,因此它可以访问元素并打印它们。

答案 1 :(得分:0)

运算符<<是二进制运算符,这意味着它在左侧需要一个操作数,而在右侧则需要一个操作数。例如,当您编写cout << "Hello World"时,左侧操作数为cout,类型为ostream,右侧操作数为字符串"Hello World"。类似地,要为您的一个类重载运算符,您必须定义将是左侧操作数和右侧操作数,就像重载plus(+)运算符或任何其他二进制运算符一样,需要您定义什么是操作数。因此,os应该用在您写过cout的地方,如上一个答案中已经指出的那样,如果在左侧,它将带有“值” cout操作符为cout

至于返回os,这样做的原因是为了能够链接多个打印语句。可以使用整数来写cout << 3 << 5;,因为首先对第一个<<运算符求值,打印3,然后返回os并将其作为左侧操作数传递给下一个{{1} }。如果重载运算符未返回<<,则将无法处理您的类的对象。

最后,正如已经说过的那样,重载应该是该类的朋友,因为重载需要访问其私有成员,并且由于它不是该类的成员函数,因此除非有重载,否则它不能拥有它

相关问题