Meredith L. Patterson ([identity profile] maradydd.livejournal.com) wrote in [personal profile] maradydd 2009-05-14 03:48 pm (UTC)

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.

Post a comment in response:

If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org