I love const_cast and it loves me back. (It's hard to go wrong adding const to things as long as you're confident that it's okay for them to be immutable. These totally are.)
My trick for this one is that 'const' applies to whatever it comes directly after (with one exception, on which more shortly). For instance:
int const* foo - foo is a pointer to const int int * const bar - bar is a const pointer to int int const * const baz - baz is a const pointer to const int
The one exception is that if there's an empty string to the left of 'const', then 'const' applies to whatever token is directly to the right of it. Thus, int const* foo and const int* foo are both pointers to const int.
This also applies to const functions, where you can end up with crazy stuff like
const int*const Method3(const int*const&)const;
as described here (http://duramecho.com/ComputerInformation/WhyHowCppConst.html). This function takes a const reference to a pointer to const int, returns a const pointer to const int, and can't modify the object that it's a method of.
I'd personally write that declaration as int const * const Method3(int const * const &Param)const; just to make it easier to follow, but when I'm not doing something that awkward-looking, I tend to put the 'const' first, just like most people. It's probably not good style though.
Whoops, you've just exposed some sloppy thinking on my part. I should have said "reference to const pointer to const int", following the what-comes-before-it rule -- there's a const after the *, and the other one precedes the int but there's nothing before that, so it applies to the int.
For some reason, in my head that parses as "const reference". I suspect g++ error messages have something to do with it.
no subject
int const* foo - foo is a pointer to const int
int * const bar - bar is a const pointer to int
int const * const baz - baz is a const pointer to const int
The one exception is that if there's an empty string to the left of 'const', then 'const' applies to whatever token is directly to the right of it. Thus, int const* foo and const int* foo are both pointers to const int.
This also applies to const functions, where you can end up with crazy stuff like
const int*const Method3(const int*const&)const;
as described here (http://duramecho.com/ComputerInformation/WhyHowCppConst.html). This function takes a const reference to a pointer to const int, returns a const pointer to const int, and can't modify the object that it's a method of.
I'd personally write that declaration as int const * const Method3(int const * const &Param)const; just to make it easier to follow, but when I'm not doing something that awkward-looking, I tend to put the 'const' first, just like most people. It's probably not good style though.
no subject
isn't "const reference" inherently redundant? (i'm a total c++ newb, so i'm honestly asking)
no subject
For some reason, in my head that parses as "const reference". I suspect g++ error messages have something to do with it.
Thanks for the catch!
no subject