4 ms·
C++ is one of the few languages where in "foo->perform()" the -> and () can be something completely different than what you expect. There's almost nothing in t
by wvenable 16y ago
C++ is one of the few languages where in "foo->perform()" the -> and () can be something completely different than what you expect. There's almost nothing in that simple statement you can be completely sure of.
- tptacek 16y agoThat's a true and valid argument but beware that any C++ advocate could knock it down by saying "then the kernel code standard should reject things like that".
- varjag 16y agoAnd when you reject things like that you end up with C anyway.
- sanderjd 16y agoMeh, this is a straw-man. Conventionally rejecting overridden -> and () operators is a very long reach from ending up back at C. Disallowing operator overriding in general (as opposed to specific operators), namespaces, and classes would be much closer, and it still wouldn't be all that close to C (reinterpret_cast, templates...) There is a goodly sized amount of gray area between the two. Or did you mean your "things like that" to be much broader than my interpretation?
- albertzeyer 16y agoSimilar thing with C macros. (Note that my point here is that for sane/good code, you normally can be sure that it does what it looks like -- for C++ just the same way as for C.)
- NickPollard 16y agoThey can be, but they shouldn't be. Operator overloading is useful when you want to do something that is semantically similar but implementationally different. Even mathematics has different processes for the same operators on different types. If you see 'a * b' you should be able to assume that it's multiplying two variables, but if those two are real numbers it's different than if they're two matrices of real numbers, for example. Implementing a Matrix class that overrides * to imlement matrix multiplication seems perfectly fine for me - It's just the same as implementing a Multiply(a, b) method. Just as someone who writes a Multiply method could in theory make it do anything, it is assumed by reading the word 'Multiply' that that is what it does. Just as someone who makes a Multiply() function that does something other than multiply is an idiot, so is someone who overrides operator * to do something other than multiply.
- wvenable 16y agoThe STL overrides the bit shifting operators for output and the deference operator for accessing the current item in iterators.