c++ - Should operator<< be implemented as a friend or as a member function? -


This is basically a question, what is the operator < & Lt; Is there a "correct" way to implement ? Reading I can see something like this:

  Buddy Bull Operator & lt; & Lt; (Obj const & lhs, obj const & amp; rhs);   

Something like

  ostream & amp; Operator & lt; & Lt; (Obj const & amp; rhs);   

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

My personal case is:

  Friends Ocerem & amp; Operator & lt; & Lt; (Ostream and OS, Conf. Paragraph and P) {Returns OS & lt; & Lt; P.to_str (); }   

But maybe I could:

  ostream & amp; Operator & lt; & Lt; (Ostream and OS) {Return to OS & lt; & Lt; Paragraph }   

On what basis should I base this decision?

Note :

  Para: To_str = (return paragraph)   

where a string of paragraphs is.

The problem here is in the interpretation of your article.

This article is about someone who is correctly defining the speech relation operator.

Operator:

  • Equality == and more! =
  • Relationships & lt; & Gt; & Lt; = & Gt; =

    These operators should return a hair because they are comparing two identical types of objects. It is usually easy to define these operators as part of the classroom, because the class is an automatic friend of itself, so objects in the type paragraphs can examine each other (even each one Other personal members can also be).

    There is an argument to create these free standing tasks because it should automatically convert both the conversions if they are not of the same type, whereas member functions only allow themselves to be changed. I get this a paper man argument because you really do not want to manually convert itself to the first place (usually), but if there is anything you want (I do not recommend it) then it can be beneficial to stand comparative free.

    Stream operator:

    • Operator & lt; & Lt; Output
    • Operator & gt; & Gt; Input

      When you use them as a stream operator (rather than a binary shift), the first parameter is a stream. Since you do not have access to the stream object (it is not yours to modify) these members can not be operator, which they should be out of class. Thus they should either be friends of class or use any public law that will be streaming for you.

      It is also traditional for these objects to return the context of a traditional object so that you can simultaneously chain chain operations together.

        #include & lt; Iostream & gt; Class para {public: clear paragraph (std :: string const & init): m_para (init) {} std :: string const & amp; To_str () const {return m_para; } BULL operator == (paragraph const & amp; rhs) const {returns m_para == rhs.m_para; } Bool Operator! = (Paragraph const & amp; rhs) const defined {//! = = Operator operator in case of operator! (This-> operator == (rhs)); } The Bull Operator & lt; (Paragraph form & ra) const {return m_para & lt; Rhs.m_para; } Private: Friend std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & os, const paragraph & amp; p); Std :: string m_para; }; Std :: Ostream & amp; Operator & lt; & Lt; (Std :: ostream & os, const paragraph & amp; amp; nbsp;); {return OS & lt; & Lt; P.to_str (); } Int main () {paragraph P ("plop"); Article q (p); Std :: cout & lt; & Lt; P & LT; & Lt; Std :: endl & lt; & Lt; (P == q) & lt; & Lt; Std :: endl; }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -