Post by R.WieserNow I'm again confused. You are using a 'const' modifier after the
variable-name, while David L. gave me a link showing both of them before it
[yours] const int *pointer_to_x() const { return &x; }
[His link] const <type of pointer>* const <name of pointer>
Does that mean you could get actually three 'const' modifiers in there ?
Unfortunately yes. For pointer variables there are two usages
1) const char *pText;
2) char * const pText;
which mean very different things.
The pointer pText defines a location in memory.
case 1 means that you can't change the data which is stored at that memory
location (at least not by using the pointer pText to get access to the data)
case 2 means that you can't change the variable pText to make it point
somewhere else.
If one is being precise with language then these are
(1) a pointer to a const
(2) a const pointer
but unfortunately, in everyday speech, people are seldom precise about this.
[And the first is, in my experience, much more common than the second:
Windows even defines LPCSTR to be 'const char *'.]
(3) The third usage is very different: it is not for variables (per se) but
for class member functions, eg
class MYCLASS
{
int x;
public:
...
int myMethod() const;
};
means that within the member function myMethod() nothing is allowed to
happen which changes any member variable of the instance of MYCLASS. More
precisely it means that the 'this' pointer within myMethod() is a pointer to
a const object.
int MYCLASS::myMethod() const
{
x = 3; // ERROR method is const
MYCLASS *p1 = this; // ERROR this is of type "const
MYCLASS *"
const MYCLASS *p2 = this; // OK
return x++; // ERROR method is const
}
HTH
Dave
-- David Webber
Mozart Music Software
http://www.mozart.co.uk/
[mixing] const int *const pointer_to_x() const { return &x; }
No clue what this would result in though .... :-\
If not, how is yours different from his example ? They seem to do the same
thing.
Post by R.WieserI regard becoming familiar with the uses of 'const' as one of
the most important things anyone learning C or C++ must do.
:-) Than I'm lucky I noticed them this early in my wrestling with some
source-code, ain't I ?
Post by R.WieserIf all the things which should be 'const' *are* 'const' then you can
maintain vast chunks of code, knowing that they can't corrupt your
data, because the compiler would have given you an error message.
Yes, that will be very helpfull indeed.
Regards,
Rudy Wieser
Post by R.WieserPost by R.WieserI think I understand that. One question though: is that only for the member
variables, or can't we use local variables, calculations with them and so on
either ? <
You can always use local variables in member functions, but they're local
and can't be seen outside.
Post by R.WieserPost by David WebberIf you have a const *before* a function name, then that refers
to the return type - usually a const pointer of const reference.
I take it that it than behaves equal to a const infront of a normal
variable.
A 'const pointer' in the sense
const char *pText = "Hello";
means you can't change what is at the location pText in memory - ie you
can't replace the content "Hello" at that point. If you try - eg with
strcpy( pText, "Fred" );
the compiler will give you an error. If class member functions return
pointers, the functions often come in pairs (with the same name) in the form
class MYCLASS
{
int x;
. ...
int * pointer_to_x() { return &x; }
const int *pointer_to_x() const { return &x; }
};
Note the *two* uses of const in the second one. The first one lets you
change the contents of your class instance from outside, the second one
doesn't.
I regard becoming familiar with the uses of 'const' as one of the most
important things anyone learning C or C++ must do. If all the things which
should be 'const' *are* 'const' then you can maintain vast chunks of code,
knowing that they can't corrupt your data, because the compiler would have
given you an error message. And in particular the MFC library is a very
very bad example: all sorts of things which should logically be const are
not.
Dave
-- David Webber
Mozart Music Software
http://www.mozart.co.uk/