Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I understood the concept of overloading , but I am not clear why some operators can not be overloaded .I did not get any clear answer of it.


What I have tried:

I understood the concept of overloading , but I am not clear why some operators can not be overloaded .I did not get any clear answer of it.
Posted
Updated 18-Oct-16 21:14pm
Comments
Richard MacCutchan 16-Oct-16 11:49am    
Because those are the rules of the language. You can read the full explanation in any of the published documentation.
ROBIN KUMAR 16-Oct-16 14:30pm    
thanks..

:: can't be overloaded because it is the scope qualifier (or name resolution) operator - it specifies a class and namespace name and it's not available at a class level because it helps to specify which class you are referring to.

Similarly, "." and "*" can't be overloaded because they specify access, not an operation per se. Dot specifies a member of a class, and star is a pointer resolution operator.

?: can't be overridden because it isn't really an operator: it's syntactic sugar for an if...then...else statement that returns a value.
 
Share this answer
 
Comments
Stefan_Lang 19-Oct-16 2:39am    
Well, "*" _can_ be overloaded. But you are probably referring to the unary operator, not the binary one ;-p

P.S.: in fact, '*' (unary) can also be overloaded, you probably had '.*' in mind (as did I)
see https://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading#Operators_which_cannot_be_overloaded
As a general answer, for "some" of these operators, overloading isn't entirely unthinkable, but Stroustrup and the C++ committee didn't see a meaningful use for them, while potentially introducing ambiguities or unexpected side effects.

(Personally, I think it's a shame that the only ternary operator, '?:' can't be overloaded - I'm sure I could find a use for that ;-) )

See C++ Programming/Operators/Operator Overloading - Wikibooks, open books for an open world[^] and Stroustrup: C++ Style and Technique FAQ: Why can't I overload ...[^]
 
Share this answer
 
Comments
CPallini 19-Oct-16 3:17am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900