Solved

function overloading

Posted on 2000-03-15
5
213 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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 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