Friend and Operator []

V

Victor Bazarov

Avinash said:
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
 
J

John Carson

Mike Wahler said:
Avinash said:
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.
 
T

tom_usenet

Mike Wahler said:
Avinash said:
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
 
J

John Carson

tom_usenet said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top