473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Friend and Operator []

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

Thanking You.
Avinash
Jul 19 '05 #1
5 5567
"Avinash" <ra******@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
Jul 19 '05 #2
Avinash <ra******@yahoo.com> wrote in message
news:81**************************@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

Jul 19 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:bd**********@slb9.atl.mindspring.net
Avinash <ra******@yahoo.com> wrote in message
news:81**************************@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)

Jul 19 '05 #4
On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
<do***********@datafast.net.au> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:bd**********@slb9.atl.mindspring.net
Avinash <ra******@yahoo.com> wrote in message
news:81**************************@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
Jul 19 '05 #5
"tom_usenet" <to********@hotmail.com> wrote in message
news:3e****************@news.easynet.co.uk
On Wed, 25 Jun 2003 00:20:23 +1000, "John Carson"
<do***********@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)

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
5
by: Daniel T. | last post by:
The goal is to make a friend function of an inner template class... template < typename T > class Foo { public: class Bar { friend bool operator==( const typename Foo<T>::Bar& lhs, const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.