Solved

function overloading

Posted on 2000-03-15
5
209 Views
Last Modified: 2010-04-02
why c++ does not allow =,(),[],-> to be overloaded as friend function?
0
Comment
Question by:jamanat
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
Comment Utility
When overload any operator, it is required that at least one of the operands be a user-defined class type.  However, with these operators there are additional restructions, for example with operator = the destination operand (left one) has to be a user defined class, so to force that to occur it is made a member function.  (if it was a friend you could write

operator =(int &Destination,const SomeClass &Source);

an operator that has 1 user defined class type, but the type is not the destination.

continues
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
For operator (), it must be a member because you need an object to place to the left of the ().  Without that object you would have confussion, for example if you had

int operator(int i) { return i +1; };

int x = 1;
int y = (int) x;

does that code just cast an int to an int, which does nothing, or does it call that overloaded operator ()?  You can't tell, but as a member function an object will always appear to the left of the parenthesis to make this clear.

continues
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
operator [] is simlar to (), agan it is to force you to put an object to the left of the [].  Although in this case there would be no syntacitc confussions as

int y = [x];

is not currently legal.

I'm not sure why operator -> would have to have this restriction.  it seems like it could be written as a non-member that takes a single parameter that must be a user-defined class type.  

0
 

Author Comment

by:jamanat
Comment Utility
Hey thanks for answering my question. But I need some more help on this. Can you mail me on jamanat@usa.net?
 please....
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Why e-mail?  Is there any reason you cannot ask here?   Information posted here will be saved in EE's database for future queries.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now