Link to home
Start Free TrialLog in
Avatar of jamanat
jamanat

asked on

function overloading

why c++ does not allow =,(),[],-> to be overloaded as friend function?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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
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.  

Avatar of jamanat

ASKER

Hey thanks for answering my question. But I need some more help on this. Can you mail me on jamanat@usa.net?
 please....
Why e-mail?  Is there any reason you cannot ask here?   Information posted here will be saved in EE's database for future queries.