Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Friend and Operator []

Reply
Thread Tools

Friend and Operator []

 
 
Avinash
Guest
Posts: n/a
 
      06-24-2003
Hi,
Why Overloaded operator [] cannot be a friend ?

Thanking You.
Avinash
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-24-2003
"Avinash" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      06-24-2003
Avinash <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi,
> Why Overloaded operator [] cannot be a friend ?


Why can't birds fly?

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

-Mike



 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      06-24-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message
news:bd9ltr$hf9$(E-Mail Removed)
> Avinash <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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)

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      06-24-2003
On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
<(E-Mail Removed)> wrote:

>"Mike Wahler" <(E-Mail Removed)> wrote in message
>news:bd9ltr$hf9$(E-Mail Removed)
>> Avinash <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) om...
>> > 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:perator[](int);
};

Tom
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      06-24-2003
"tom_usenet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
> <(E-Mail Removed)> 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: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:perator[](int);
int array[10];
};

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

int& A: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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments