如果运营商<被实现为朋友还是作为成员函数? [英] Should operator<< be implemented as a friend or as a member function?

查看:93
本文介绍了如果运营商<被实现为朋友还是作为成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是一个问题,是否有一个正确的方式来实现 operator<<
阅读我可以看到像:

That's basically the question, is there a "right" way to implement operator<< ? Reading this I can see that something like:

friend bool operator<<(obj const& lhs, obj const& rhs);

优先于

ostream& operator<<(obj const& rhs);

但我不知道为什么要使用其中一个。

But I can't quite see why should I use one or the other.

我的个人案例是:

friend ostream & operator<<(ostream &os, const Paragraph& p) {
    return os << p.to_str();
}

但我可能会这样做:

ostream & operator<<(ostream &os) {
    return os << paragraph;
}

我应该根据什么理由?

 Paragraph::to_str = (return paragraph) 

其中paragraph是一个字符串。

where paragraph's a string.

推荐答案

这里的问题在于您对链接的文章的解释。

The problem here is in your interpretation of the article you link.

这篇文章是关于正确定义bool关系运算符的问题的人。

This article is about somebody that is having problems correctly defining the bool relationship operators.

运算符:


  • 平等==和!=

  • >< => =

这些运算符在比较两个相同类型的对象时应返回一个bool。通常最容易将这些操作符定义为类的一部分。这是因为一个类本身是一个自己的朋友,因此类型Paragraph的对象可以检查对方(甚至每个其他私人成员)。

These operators should return a bool as they are comparing two objects of the same type. It is usually easiest to define these operators as part of the class. This is because a class is automatically a friend of itself so objects of type Paragraph can examine each other (even each others private members).

有一个参数使这些独立的功能,因为这允许自动转换转换两侧,如果他们不是相同的类型,而成员函数只允许自动转换rhs。我发现这是一个纸人的论点,因为你不真的想要自动转换发生在第一位(通常)。但是,如果这是你想要的(我不推荐它),那么使比较器自由站立是有利的。

There is an argument for making these free standing functions as this lets auto conversion convert both sides if they are not the same type, while member functions only allow the rhs to be auto converted. I find this a paper man argument as you don't really want auto conversion happening in the first place (usually). But if this is something you want (I don't recommend it) then making the comparators free standing can be advantageous.

流操作符:


  • 运算符<<输出

  • operator >> input

shift)第一个参数是一个流。因为你没有访问流对象(它不是你的修改),这些不能是成员运算符,他们必须在类外部。因此,他们必须是类的朋友,或者访问一个公共方法,为你做流。

When you use these as stream operators (rather than binary shift) the first parameter is a stream. Since you do not have access to the stream object (its not yours to modify) these can not be member operators they have to be external to the class. Thus they must either be friends of the class or have access to a public method that will do the streaming for you.

这些对象也是传统的返回一个引用到流对象,以便可以将流操作链接在一起。

It is also traditional for these objects to return a reference to a stream object so you can chain stream operations together.

#include <iostream>

class Paragraph
{
    public:
        explicit Paragraph(std::string const& init)
            :m_para(init)
        {}

        std::string const&  to_str() const
        {
            return m_para;
        }

        bool operator==(Paragraph const& rhs) const
        {
            return m_para == rhs.m_para;
        }
        bool operator!=(Paragraph const& rhs) const
        {
            // Define != operator in terms of the == operator
            return !(this->operator==(rhs));
        }
        bool operator<(Paragraph const& rhs) const
        {
            return  m_para < rhs.m_para;
        }
    private:
        friend std::ostream & operator<<(std::ostream &os, const Paragraph& p);
        std::string     m_para;
};

std::ostream & operator<<(std::ostream &os, const Paragraph& p)
{
    return os << p.to_str();
}


int main()
{
    Paragraph   p("Plop");
    Paragraph   q(p);

    std::cout << p << std::endl << (p == q) << std::endl;
}

这篇关于如果运营商&lt;被实现为朋友还是作为成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆