Friend and Operator []

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

    Friend and Operator []

    Hi,
    Why Overloaded operator [] cannot be a friend ?

    Thanking You.
    Avinash
  • Victor Bazarov

    #2
    Re: Friend and Operator []

    "Avinash" <rash_avi@yahoo .com> wrote...[color=blue]
    > Why Overloaded operator [] cannot be a friend ?[/color]

    Wrong question. Overloaded operator[] can be a friend.
    And if you get a compiler error, then read FAQ 5.8.

    Victor


    Comment

    • Mike Wahler

      #3
      Re: Friend and Operator []

      Avinash <rash_avi@yahoo .com> wrote in message
      news:81cbd42c.0 306240531.480f6 f54@posting.goo gle.com...[color=blue]
      > Hi,
      > Why Overloaded operator [] cannot be a friend ?[/color]

      Why can't birds fly?

      What C++ book(s) are you reading?

      -Mike



      Comment

      • John Carson

        #4
        Re: Friend and Operator []

        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
        news:bd9ltr$hf9 $1@slb9.atl.min dspring.net[color=blue]
        > Avinash <rash_avi@yahoo .com> wrote in message
        > news:81cbd42c.0 306240531.480f6 f54@posting.goo gle.com...[color=green]
        > > Hi,
        > > Why Overloaded operator [] cannot be a friend ?[/color]
        >
        > Why can't birds fly?
        >
        > What C++ book(s) are you reading?
        >
        > -Mike[/color]

        I don't know about the OP, but I read the following in Stroustrup (TCPL, p.
        287):

        "An operator[]() must be a member function."

        In Lippman's C++ Primer (p. 754), I read:

        "A subscript operator must be defined as a class member function."

        I also read in the C++ standard:

        13.5.5 Subscripting
        1 operator[] shall be a non-static member function with exactly one
        parameter.


        --
        John Carson
        1. To reply to email address, remove donald
        2. Don't reply to email address (post here instead)

        Comment

        • tom_usenet

          #5
          Re: Friend and Operator []

          On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
          <donaldquixote@ datafast.net.au > wrote:
          [color=blue]
          >"Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
          >news:bd9ltr$hf 9$1@slb9.atl.mi ndspring.net[color=green]
          >> Avinash <rash_avi@yahoo .com> wrote in message
          >> news:81cbd42c.0 306240531.480f6 f54@posting.goo gle.com...[color=darkred]
          >> > Hi,
          >> > Why Overloaded operator [] cannot be a friend ?[/color]
          >>
          >> Why can't birds fly?
          >>
          >> What C++ book(s) are you reading?
          >>
          >> -Mike[/color]
          >
          >I don't know about the OP, but I read the following in Stroustrup (TCPL, p.
          >287):
          >
          >"An operator[]() must be a member function."
          >
          >In Lippman's C++ Primer (p. 754), I read:
          >
          >"A subscript operator must be defined as a class member function."
          >
          >I also read in the C++ standard:
          >
          >13.5.5 Subscripting
          >1 operator[] shall be a non-static member function with exactly one
          >parameter.[/color]

          None of that stops operator[] from being a friend:

          struct A
          {
          void operator[](int){}
          };

          struct B
          {
          friend void A::operator[](int);
          };

          Tom

          Comment

          • John Carson

            #6
            Re: Friend and Operator []

            "tom_usenet " <tom_usenet@hot mail.com> wrote in message
            news:3ef862fc.1 03986265@news.e asynet.co.uk[color=blue]
            > On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
            > <donaldquixote@ datafast.net.au > wrote:[color=green]
            > >
            > > I don't know about the OP, but I read the following in Stroustrup
            > > (TCPL, p. 287):
            > >
            > > "An operator[]() must be a member function."
            > >
            > > In Lippman's C++ Primer (p. 754), I read:
            > >
            > > "A subscript operator must be defined as a class member function."
            > >
            > > I also read in the C++ standard:
            > >
            > > 13.5.5 Subscripting
            > > 1 operator[] shall be a non-static member function with exactly one
            > > parameter.[/color]
            >
            > None of that stops operator[] from being a friend:
            >
            > struct A
            > {
            > void operator[](int){}
            > };
            >
            > struct B
            > {
            > friend void A::operator[](int);
            > };
            >
            > Tom[/color]


            You are right. Being a member function and being a friend are not mutually
            exclusive.

            I was thinking that being a friend was useless anyway for a subscript
            operator because it could not take an object as an argument and hence could
            not access any object's members. But of course there are other ways to make
            a variable available to an operator besides passing the variable as an
            argument, e.g.,

            class B;
            struct A
            {
            A();
            ~A();
            B *pb;
            int& operator[](int);
            };

            class B
            {
            friend int& A::operator[](int);
            int array[10];
            };

            A::A() : pb(new B){}
            A::~A(){delete pb;}

            int& A::operator[](int n)
            { return pb->array[n];}


            int main(int argc, char* argv[])
            {
            A a;
            // uses A's subscript operator to access B's private member data
            a[0] = 5;

            return 0;
            }


            --
            John Carson
            1. To reply to email address, remove donald
            2. Don't reply to email address (post here instead)

            Comment

            Working...