Solved

function overloading

Posted on 2000-03-15
5
214 Views
Last Modified: 2010-04-02
why c++ does not allow =,(),[],-> to be overloaded as friend function?
0
Comment
Question by:jamanat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 2619738
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
ID: 2619758
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
ID: 2619776
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
ID: 2632864
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
ID: 2633057
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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