?
Solved

function overloading

Posted on 2000-03-15
5
Medium Priority
?
217 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 90 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 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