In c++, why we can overload operator ->
and can't overload operator .
?
Similarly, why we can overload operator ->*
and can't overload operator .*
?
I will be appreciated if you can help me!
Asked
Active
Viewed 577 times
4

Jarod42
- 203,559
- 14
- 181
- 302

Invisible_man
- 85
- 6
-
4Have you read https://isocpp.org/blog/2016/02/a-bit-of-background-for-the-operator-dot-proposal-bjarne-stroustrup? – UKMonkey Oct 25 '17 at 09:24
-
5Because the C++ standard specifies so. – Basile Starynkevitch Oct 25 '17 at 09:24
-
1Because it'd be utter madness if we could. Just look at all the nastiness `operator&` causes. – StoryTeller - Unslander Monica Oct 25 '17 at 09:29
-
3@BasileStarynkevitch Sigh. Why does the standard specify so? – juanchopanza Oct 25 '17 at 09:29
-
2Because no one has made a coherent proposal for such feature. Try and see why it's hard. – n. m. could be an AI Oct 25 '17 at 09:33
1 Answers
4
Stroustrup actually has this as an FAQ question on his website. To quote his answer:
Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by .
This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best
(Note that Stroustup himself has since proposed allowing overloading of operator.
, as have others, so we may see it in a future version of the language.)

Community
- 1
- 1

Tristan Brindle
- 16,281
- 4
- 39
- 82