Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't everything be overloaded in C++?

It is reasonable that sizeof and typeid cannot be overloaded, but I can't see the harm in overloading ?:, .* and .. Are there technical reasons for this?

like image 716
Tim Avatar asked Mar 15 '11 17:03

Tim


1 Answers

To quote Bjarne Stroustrup:

There is no fundamental reason to disallow overloading of ?:. I just didn't see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed.

...

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.

Source

like image 156
bta Avatar answered Oct 13 '22 21:10

bta