Solved

function overloading

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 clear a vector as well as how to detect empty vectors in C++.
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.

914 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

20 Experts available now in Live!

Get 1:1 Help Now