Re: constant references
Brian,
No. It guarantees a LOT. I use it regularly on large projects. It
helps enforce design issues where certain elements of my code just
should not be allowed to mess with certain data. It is extremely
important. Also, for all the elsewhere mentioned reasons in another
post of mine, such as catching bugs at compile time, make it
priceless. It just forces good programming. How can you beat that?
Yes, you can cast it away -- but not with 'normal' C++ code. Casting
away const is not allowed. You can get around it with fancy C++ code
to trick the compiler into accessing that memory, but it's really just
a high level way of writing assembly. So, yes, it's not 100% bullet
proof. But, that's not the point. The point is the immense gain you
get, from being forced to write good code, from using it.
Just because someone can pick a lock, it doesn't mean you should stop
locking your door. Now imagine a lock that forces the door to be
locked, and forces you to have the keys on you, every time you leave.
Anything that forces good habits is a good idea.
Really? I don't really know. It's just sad that it doesn't exist...
Ok. Something else for me to learn. Any places I can get started on
researching this? Any help is appreciated.
Thanks for the suggestion. This was mentioned above, and for
something where it is critical (i.e. it warrants the extra hassle
involved), it certainly could be used.
Zytan
Brian,
Even in C++ const didn't guarantee much since the reference's
constness could be casted away.
constness could be casted away.
helps enforce design issues where certain elements of my code just
should not be allowed to mess with certain data. It is extremely
important. Also, for all the elsewhere mentioned reasons in another
post of mine, such as catching bugs at compile time, make it
priceless. It just forces good programming. How can you beat that?
Yes, you can cast it away -- but not with 'normal' C++ code. Casting
away const is not allowed. You can get around it with fancy C++ code
to trick the compiler into accessing that memory, but it's really just
a high level way of writing assembly. So, yes, it's not 100% bullet
proof. But, that's not the point. The point is the immense gain you
get, from being forced to write good code, from using it.
Just because someone can pick a lock, it doesn't mean you should stop
locking your door. Now imagine a lock that forces the door to be
locked, and forces you to have the keys on you, every time you leave.
Anything that forces good habits is a good idea.
I think it was left out of C# because
the cost versus benefit of it tipped toward the unfavorable
direction. Implementing const reference parameters would add a
substantial amount of complexity to the language.
the cost versus benefit of it tipped toward the unfavorable
direction. Implementing const reference parameters would add a
substantial amount of complexity to the language.
The best way to guarantee that an object's state cannot be changed is
to make it immutable.
to make it immutable.
researching this? Any help is appreciated.
Another strategy would be to use an interface or proxy object.
Though, I don't think either could absolutely be enforced at compile
time. A proxy object would probably be the best of two.
Though, I don't think either could absolutely be enforced at compile
time. A proxy object would probably be the best of two.
something where it is critical (i.e. it warrants the extra hassle
involved), it certainly could be used.
Zytan
Comment