Friend and Operator []

Discussion in 'C++' started by Avinash, Jun 24, 2003.

  1. Avinash

    Avinash Guest

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

    Thanking You.
    Avinash
     
    Avinash, Jun 24, 2003
    #1
    1. Advertising

  2. "Avinash" <> wrote...
    > Why Overloaded operator [] cannot be a friend ?


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

    Victor
     
    Victor Bazarov, Jun 24, 2003
    #2
    1. Advertising

  3. Avinash

    Mike Wahler Guest

    Avinash <> wrote in message
    news:...
    > Hi,
    > Why Overloaded operator [] cannot be a friend ?


    Why can't birds fly?

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

    -Mike
     
    Mike Wahler, Jun 24, 2003
    #3
  4. Avinash

    John Carson Guest

    "Mike Wahler" <> wrote in message
    news:bd9ltr$hf9$
    > Avinash <> wrote in message
    > news:...
    > > Hi,
    > > Why Overloaded operator [] cannot be a friend ?

    >
    > Why can't birds fly?
    >
    > What C++ book(s) are you reading?
    >
    > -Mike


    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)
     
    John Carson, Jun 24, 2003
    #4
  5. Avinash

    tom_usenet Guest

    On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
    <> wrote:

    >"Mike Wahler" <> wrote in message
    >news:bd9ltr$hf9$
    >> Avinash <> wrote in message
    >> news:...
    >> > Hi,
    >> > Why Overloaded operator [] cannot be a friend ?

    >>
    >> Why can't birds fly?
    >>
    >> What C++ book(s) are you reading?
    >>
    >> -Mike

    >
    >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.


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

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

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

    Tom
     
    tom_usenet, Jun 24, 2003
    #5
  6. Avinash

    John Carson Guest

    "tom_usenet" <> wrote in message
    news:
    > On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
    > <> wrote:
    > >
    > > 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.

    >
    > None of that stops operator[] from being a friend:
    >
    > struct A
    > {
    > void operator[](int){}
    > };
    >
    > struct B
    > {
    > friend void A::eek:perator[](int);
    > };
    >
    > Tom



    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::eek:perator[](int);
    int array[10];
    };

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

    int& A::eek:perator[](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)
     
    John Carson, Jun 24, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Bhardwaj
    Replies:
    8
    Views:
    1,049
    Andrey Tarasevich
    Jul 14, 2003
  2. CoolPint
    Replies:
    3
    Views:
    518
    Victor Bazarov
    Feb 13, 2005
  3. Patrick Labatut

    Templates and friend operator

    Patrick Labatut, Sep 27, 2005, in forum: C++
    Replies:
    3
    Views:
    343
  4. Rock
    Replies:
    4
    Views:
    463
    Jim Langston
    Nov 20, 2005
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page