3 ms·
> With C++, you don’t see the writes, because it says `some_func(whatever,the_int,SOME_FLAG);` and it can still write the_int if it’s a reference. In C, it woul
by comex 16y ago
> With C++, you don’t see the writes, because it says
`some_func(whatever,the_int,SOME_FLAG);` and it can still write the_int if it’s a reference. In C, it would be &the_int, which is easily recognizable.
I don't understand why people do this. C++ has not abolished pointers; it's still possible to require the &, and it increases readability considerably; why ever use references (to value types; objects are different) as function parameters?
- arghnoname 16y agoreference to const gives the benefit of passing a pointer, efficiency wise, but prevents the called function from modifying the contents it points to.
- jpr 16y agoAFAICT, that's achievable with pointers, too.
- arghnoname 16y agoOf course it is, with the minor syntactic addition of callers passing in the memory location. References aren't a _necessary_ feature of function parameters and if the reference is going to be modified, a pointer is more clear and should be used. But there is a utility in allowing references to exist in the language for overloaded operators, so you can overload a subscript in a collection and assign to it, such as foo[x] = bar, where foo is a collection of bar objects. They could have restricted references to just these sorts of use cases and when using foo[x] as an argument, require the user to type &foo[x], but once references are in the language allowing them as parameters seems like a reasonable enough decision to me.