friend oper<<

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vineoff

    #1

    friend oper<<

    Why do I have to declare my oper<< as friend in my class as follows:

    class A {
    public:
    friend std::ostream& operator<<(std: :ostream& out, const A& a)
    { return out << "foo"; }
    };

  • marco

    #2
    Re: friend oper&lt;&lt;

    vineoff ha scritto:[color=blue]
    > Why do I have to declare my oper<< as friend in my class as follows:
    >
    > class A {
    > public:
    > friend std::ostream& operator<<(std: :ostream& out, const A& a)
    > { return out << "foo"; }
    > };
    >[/color]
    Why did you do? It is necessary only if you want to have access to
    non-public members of class A within operator <<.

    In this case you can write
    class A {};
    std::ostream& operator<<(std: :ostream& out, const A& a)
    { return out << "foo"; }

    Comment

    • Rolf Magnus

      #3
      Re: friend oper&lt;&lt;

      vineoff wrote:
      [color=blue]
      > Why do I have to declare my oper<< as friend in my class as follows:
      >
      > class A {
      > public:
      > friend std::ostream& operator<<(std: :ostream& out, const A& a)
      > { return out << "foo"; }
      > };[/color]

      No reason, i.e. you don't have to.

      Comment

      • vineoff

        #4
        Re: friend oper&lt;&lt;

        But can you show me an example without friend keyword. If I just remove
        it now, it won't compile

        Comment

        • xian

          #5
          Re: friend oper&lt;&lt;


          vineoff wrote:[color=blue]
          > But can you show me an example without friend keyword. If I just remove
          > it now, it won't compile[/color]

          like this :)

          #include<iostre am>

          class A {
          public:
          int val;
          };
          std::ostream& operator<<(std: :ostream& out, const A& a)
          {
          return out << a.val;
          }

          int main()
          {
          A a;
          std::cout << a;
          }

          And Scott Meyers said: Don't let operator << or operator >> be the
          members. And if you want to access the private members, let it be a
          friend.

          :)

          Comment

          • Rolf Magnus

            #6
            Re: friend oper&lt;&lt;

            vineoff wrote:
            [color=blue]
            > But can you show me an example without friend keyword. If I just remove
            > it now, it won't compile[/color]

            The operator must be a non-member, which it is automatically if you declare
            it as friend. So you have to write:

            class A {
            public:
            };

            std::ostream& operator<<(std: :ostream& out, const A& a)
            { return out << "foo"; }

            int main()
            {
            }

            Comment

            • vineoff

              #7
              Re: friend oper&lt;&lt;

              Why operator== i.e. can be declared as member function but operator<<
              can't?

              Comment

              • Thomas Tutone

                #8
                Re: friend oper&lt;&lt;

                vineoff wrote:[color=blue]
                > Why operator== i.e. can be declared as member function but operator<<
                > can't?[/color]

                This is an FAQ:



                It's not that you can't declare operator<<() as a member function, it's
                that it wouldn't do what you seem to think it would.

                In short, to declare operator<<() as a member function, you would have
                to modify the declaration and definition of class std::ostream, since
                the left-hand-side parameter is of type std::ostream and the
                right-hand-side parameter is the object you want to send to the
                ostream. And of course, you are not permitted to modify std::ostream
                yourself.

                If you, on the other hand, made operator<<() a member of your class
                Foo, then you would have to put Foo on the left-hand-side and the
                std::ostream on the right-hand-side when you used the operator, which
                would look very strange.

                Best regards,

                Tom

                Comment

                • Thomas Tutone

                  #9
                  Re: friend oper&lt;&lt;

                  vineoff wrote:[color=blue]
                  > Why operator== i.e. can be declared as member function but operator<<
                  > can't?[/color]

                  This is an FAQ:



                  It's not that you can't declare operator<<() as a member function, it's
                  that it wouldn't do what you seem to think it would.

                  In short, to declare operator<<() as a member function, you would have
                  to modify the declaration and definition of class std::ostream, since
                  the left-hand-side parameter is of type std::ostream and the
                  right-hand-side parameter is the object you want to send to the
                  ostream. And of course, you are not permitted to modify std::ostream
                  yourself.

                  If you, on the other hand, made operator<<() a member of your class
                  Foo, then you would have to put Foo on the left-hand-side and the
                  std::ostream on the right-hand-side when you used the operator, which
                  would look very strange.

                  Best regards,

                  Tom

                  Comment

                  • Thomas Tutone

                    #10
                    Re: friend oper&lt;&lt;

                    Thomas Tutone wrote:

                    [Quote of repetitive post snipped ...]

                    Well, I posted this ONCE at around 1:30pm New York time via Google
                    Groups, and it has appeared TWICE on usenet - once at 2:37pm, and again
                    at 3:01pm.

                    Goodness only knows what is going on at Google...

                    Best regards,

                    Tom

                    Comment

                    Working...