Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Friend and Operator [] (http://www.velocityreviews.com/forums/t267656-friend-and-operator.html)

Avinash 06-24-2003 01:31 PM

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

Thanking You.
Avinash

Victor Bazarov 06-24-2003 01:36 PM

Re: Friend and Operator []
 
"Avinash" <rash_avi@yahoo.com> 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



Mike Wahler 06-24-2003 02:15 PM

Re: Friend and Operator []
 
Avinash <rash_avi@yahoo.com> wrote in message
news:81cbd42c.0306240531.480f6f54@posting.google.c om...
> Hi,
> Why Overloaded operator [] cannot be a friend ?


Why can't birds fly?

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

-Mike




John Carson 06-24-2003 02:20 PM

Re: Friend and Operator []
 
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:bd9ltr$hf9$1@slb9.atl.mindspring.net
> Avinash <rash_avi@yahoo.com> wrote in message
> news:81cbd42c.0306240531.480f6f54@posting.google.c 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)


tom_usenet 06-24-2003 02:41 PM

Re: Friend and Operator []
 
On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
<donaldquixote@datafast.net.au> wrote:

>"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
>news:bd9ltr$hf9$1@slb9.atl.mindspring.net
>> Avinash <rash_avi@yahoo.com> wrote in message
>> news:81cbd42c.0306240531.480f6f54@posting.google.c 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::operator[](int);
};

Tom

John Carson 06-24-2003 03:18 PM

Re: Friend and Operator []
 
"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:3ef862fc.103986265@news.easynet.co.uk
> On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
> <donaldquixote@datafast.net.au> 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::operator[](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::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)



All times are GMT. The time now is 04:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.