4 ms·
There's also two places you can put "int" in a function declaration, but nobody ever seems to complain about that ;) I'm not even sure I understand the rant, t
by to3m 11y ago
There's also two places you can put "int" in a function declaration, but nobody ever seems to complain about that ;)
I'm not even sure I understand the rant, though I've been a fan of const for years so it's probably automatic by now. So, say you have a class with a const member, and you have to pass it in to a function? Well, that's a bit unusual - const members are pretty rare in my experience. But the underlying problem sounds like: you have a const object, and you want to pass it into a function. There are three possibilities, I suppose:
1. The function takes const T & or const T * , because it doesn't change that argument. So no problem.
2. The function takes T, because it doesn't change that argument (and the object is small and/or the programmer doesn't care about the copying - irrelevant issues here). So no problem.
3. The function takes T & or T * , because it changes that argument. Compilation fails! But - no problem! Because this is what you want. You passed what's supposedly a const object into a function that expected to be able to change it. Obviously compilation has to fail.
So maybe the problem is people declaring parameters as T & or T * , when they meant const T & or const T * ?
I bet if C++ had const by default, this problem would never arise. We'd be striding, right now, arm in arm, towards our glorious const-filled destiny, singing a song about it as we went.
- angersock 11y agoAs I recall isn't there also "const T const &"?
- to3m 11y agoNo, but you can have that with pointers, since the constness of the pointer itself (i.e., whether you can change the address) is separate from the object it points to (i.e., whether it might point into ROM or not). So you can have T * (variable RAM address), T * const (fixed RAM address), const T * (variable ROM/RAM address), and const T * const (fixed ROM/RAM address). (References, by contrast, can never be reseated. So const T &const doesn't make sense. But... I didn't try this right now to double check, so if it works - though I think it doesn't - don't blame me. Or, if you do, at least allow me to plead extenuating circumstances, in the form of C++.)
- angersock 11y agoThat seems right, I must've swapped that behavior with that of pointers in my head.