Discussion:
Question: "const" infront and after a function-declaration: Meaning ?
(too old to reply)
R.Wieser
2013-04-15 11:59:22 UTC
Permalink
Hello All,

I've been looking at some VC++ sourcecode, and saw some function
declarations which I do not understand because of a 'const' modifier in it.
I even saw a function declaration with one both preceeding as well as
following it.

Could anyone here explain to me why its used and what they mean (at those
two positions) ?

Regards,
Rudy Wieser
David Lowndes
2013-04-15 12:55:03 UTC
Permalink
Post by R.Wieser
Could anyone here explain to me why its used and what they mean (at those
two positions) ?
http://msdn.microsoft.com/en-GB/library/07x6b05d(v=vs.80).aspx

"When modifying a data declaration, the const keyword specifies that
the object or variable is not modifiable."

"When following a member function's parameter list, the const keyword
specifies that the function does not modify the object for which it is
invoked."

Dave
R.Wieser
2013-04-15 16:16:29 UTC
Permalink
Hello David,

Ok, the trailing 'const' means that the function itself will not change any
private (only them? what about public ones ?) members inside the class.

The starting 'const' however isn't that clear (the MS explanation does not
mention function results).

Does it mean that it, in case of the result being a pointer, protects the
actual data inside the class against changes ?. In that particular case,
can I still change the pointer itself (something I, I think, should not be
able to if the result would be a simple value/string) ?

Hmmm ... I could imagine a function declared as 'const const int* result
functionname();' One 'const' so that the data in the class can't be
changed, and another so the pointer itself cannot be changed. Does that
make sense to you ?

By the way: thanks for the response. :-)

Regards,
Rudy Wieser


-- Origional message
Post by David Lowndes
Post by R.Wieser
Could anyone here explain to me why its used and what they mean (at those
two positions) ?
http://msdn.microsoft.com/en-GB/library/07x6b05d(v=vs.80).aspx
"When modifying a data declaration, the const keyword specifies that
the object or variable is not modifiable."
"When following a member function's parameter list, the const keyword
specifies that the function does not modify the object for which it is
invoked."
Dave
David Lowndes
2013-04-15 16:27:11 UTC
Permalink
Post by R.Wieser
Ok, the trailing 'const' means that the function itself will not change any
private (only them? what about public ones ?) members inside the class.
Any of them.
Post by R.Wieser
Does it mean that it, in case of the result being a pointer, protects the
actual data inside the class against changes ?
No. It means that the thing being returned is const. :)
Post by R.Wieser
Hmmm ... I could imagine a function declared as 'const const int* result
functionname();'
Did you mean to write "const const"?
Post by R.Wieser
One 'const' so that the data in the class can't be
changed
When a class defines a method as const, it merely informs the compiler
that that method does not modify the class - which means that it can
be called on const instances of that class (or via const pointers).
Post by R.Wieser
...and another so the pointer itself cannot be changed. Does that
make sense to you ?
Not entirely.

Try some small real example code and see if that clarifies things for
you.

Dave
R.Wieser
2013-04-15 20:49:59 UTC
Permalink
Hello David,
Post by David Lowndes
No. It means that the thing being returned is const. :)
So in case of a pointer it is protected, but not the contents its pointing
to. And my guess is that the trailing 'const' wont help there either.
Post by David Lowndes
Did you mean to write "const const"?
Yes. One to protect the contents of whatever the pointer is aiming at, and
one to protect the pointer itself.

Or is there another method to protect whatever the pointer is pointing at ?
Post by David Lowndes
When a class defines a method as const, <snip>
No, that const is trailing. I really ment two of them infront. With the
one at the end a grand possible total of three.
Post by David Lowndes
Try some small real example code and see if that clarifies
things for you.
I will certainly try. It helps to know what I should be expecting though.

Regards,
Rudy Wieser
Post by David Lowndes
Post by R.Wieser
Ok, the trailing 'const' means that the function itself will not change any
private (only them? what about public ones ?) members inside the class.
Any of them.
Post by R.Wieser
Does it mean that it, in case of the result being a pointer, protects the
actual data inside the class against changes ?
No. It means that the thing being returned is const. :)
Post by R.Wieser
Hmmm ... I could imagine a function declared as 'const const int* result
functionname();'
Did you mean to write "const const"?
Post by R.Wieser
One 'const' so that the data in the class can't be
changed
When a class defines a method as const, it merely informs the compiler
that that method does not modify the class - which means that it can
be called on const instances of that class (or via const pointers).
Post by R.Wieser
...and another so the pointer itself cannot be changed. Does that
make sense to you ?
Not entirely.
Try some small real example code and see if that clarifies things for
you.
Dave
David Lowndes
2013-04-15 21:47:08 UTC
Permalink
Post by R.Wieser
So in case of a pointer it is protected, but not the contents its pointing
to.
That depends on whether you define it as a pointer to a const
variable, or a const pointer. See the links below.
Post by R.Wieser
And my guess is that the trailing 'const' wont help there either.
The use of const to declare the method as being const has nothing to
do with the return value.
Post by R.Wieser
Post by David Lowndes
Did you mean to write "const const"?
Yes. One to protect the contents of whatever the pointer is aiming at, and
one to protect the pointer itself.
See these explanations:
http://www.thegeekstuff.com/2012/06/c-constant-pointers/
http://www.cprogramming.com/reference/pointers/const_pointers.html

Dave
R.Wieser
2013-04-16 13:33:14 UTC
Permalink
Hello David L,
Post by David Lowndes
The use of const to declare the method as being const
has nothing to do with the return value.
For the return-value itself, no. But the data the pointer is referring to
could be inside the class, which the trailing 'const' has something to say
about ...
[snip links]

Thanks for those links. They did clarify the "what can I lock-down when
defining a variable/function-result" question I had.

I think I have to try what happens if a pointer points to a const-declared
variable in a class though (can the pointed-to data be changed ? Will the
compiler complain ?).

Regards,
Rudy Wieser
Post by David Lowndes
Post by R.Wieser
So in case of a pointer it is protected, but not the contents its pointing
to.
That depends on whether you define it as a pointer to a const
variable, or a const pointer. See the links below.
Post by R.Wieser
And my guess is that the trailing 'const' wont help there either.
The use of const to declare the method as being const has nothing to
do with the return value.
Post by R.Wieser
Post by David Lowndes
Did you mean to write "const const"?
Yes. One to protect the contents of whatever the pointer is aiming at, and
one to protect the pointer itself.
http://www.thegeekstuff.com/2012/06/c-constant-pointers/
http://www.cprogramming.com/reference/pointers/const_pointers.html
Dave
David Webber
2013-04-15 17:05:32 UTC
Permalink
Post by R.Wieser
I've been looking at some VC++ sourcecode, and saw some function
declarations which I do not understand because of a 'const' modifier in it.
I even saw a function declaration with one both preceeding as well as
following it.
Post by R.Wieser
Could anyone here explain to me why its used and what they mean (at those
two positions) ?

To put some flesh on David's bones with an example, consider

class MYCLASS
{
int x;
int y;

public:

MYCLASS( int a, int b ) { x=a; y=b; }
CPoint point() const { return CPoint(x,y); }

};

The construct is not (can never be) declared with a trailing const, as it
defines (changes) the member variables.

The member point() doesn't change them and so should always be declared
const, as shown.
If any code within that function tried to change x or y, the compiler would
give you an error.

If you have a const *before* a function name, then that refers to the return
type - usually a const pointer of const reference.

Dave

-- David Webber
Mozart Music Software
http://www.mozart.co.uk/
R.Wieser
2013-04-15 21:06:17 UTC
Permalink
Hello David W,
Post by David Webber
The construct is not (can never be) declared with a trailing
const, as it defines (changes) the member variables.
I 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 ?
Post by David Webber
If 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.

Regards,
Rudy Wieser.
Post by David Webber
Post by R.Wieser
I've been looking at some VC++ sourcecode, and saw some function
declarations which I do not understand because of a 'const' modifier in it.
I even saw a function declaration with one both preceeding as well as
following it.
Post by R.Wieser
Could anyone here explain to me why its used and what they mean (at those
two positions) ?
To put some flesh on David's bones with an example, consider
class MYCLASS
{
int x;
int y;
MYCLASS( int a, int b ) { x=a; y=b; }
CPoint point() const { return CPoint(x,y); }
};
The construct is not (can never be) declared with a trailing const, as it
defines (changes) the member variables.
The member point() doesn't change them and so should always be declared
const, as shown.
If any code within that function tried to change x or y, the compiler would
give you an error.
If you have a const *before* a function name, then that refers to the return
type - usually a const pointer of const reference.
Dave
-- David Webber
Mozart Music Software
http://www.mozart.co.uk/
David Webber
2013-04-15 21:53:45 UTC
Permalink
Post by R.Wieser
I 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.Wieser
Post by David Webber
If 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;
. ...
public:

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/
R.Wieser
2013-04-16 14:01:20 UTC
Permalink
Hello David W,
Post by David Webber
You can always use local variables in member functions,
but they're local and can't be seen outside.
But for the calculations result ofcourse :-)

That was what I actully tried to ask: does that trailing 'const' also signal
nothing dynamic will happen in the function (including juggling of some
numbers to generate a result). I cannot really imagine that would be the
case (it would be pretty daft), but I had to ask.
Post by David Webber
means you can't change what is at the location pText
in memory - ie you can't replace the content "Hello"
at that point.
Yes, I think I understood that one. For a variable that is. I had nu clue
how it would work with a function-result though.
Post by David Webber
Note the *two* uses of const in the second one.
Now 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 ?

[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 David Webber
I 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 David Webber
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.
Yes, that will be very helpfull indeed.

Regards,
Rudy Wieser
Post by David Webber
Post by R.Wieser
I 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.Wieser
Post by David Webber
If 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/
David Webber
2013-04-16 15:19:01 UTC
Permalink
Post by R.Wieser
Now 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.Wieser
I 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.Wieser
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.
Yes, that will be very helpfull indeed.

Regards,
Rudy Wieser
Post by R.Wieser
Post by R.Wieser
I 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.Wieser
Post by David Webber
If 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/
R.Wieser
2013-04-16 22:38:44 UTC
Permalink
Hello David W,
Post by David Webber
Unfortunately yes. For pointer variables there are two usages
[Snip]

Oh man, I see I made a couple of mistakes. :-\ I regarded the
"pointer_to_x()" parts as variables, not functions.
Post by David Webber
(1) a pointer to a const
(2) a const pointer
I (think that I) understood those. I just thought that you showed one with
a 'const' modifier right of a variable (pointer or otherwise), instead of a
function.

I that case I think I now pretty-much understand all three usages. Thanks
both of you for explaining them to me.

Regards,
Rudy Wieser
Post by David Webber
Post by R.Wieser
Now 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.
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;
...
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.Wieser
I 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.Wieser
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.
Yes, that will be very helpfull indeed.
Regards,
Rudy Wieser
Post by R.Wieser
Post by R.Wieser
I 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
Post by David Webber
on
Post by R.Wieser
either ? <
You can always use local variables in member functions, but they're local
and can't be seen outside.
Post by R.Wieser
Post by David Webber
If 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
Post by R.Wieser
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
Post by R.Wieser
should be 'const' *are* 'const' then you can maintain vast chunks of
code,
Post by R.Wieser
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/
Christian Freund
2013-06-19 08:01:52 UTC
Permalink
Usually:
const int value; // Value is const and your compiler will warn when you try
to change it
int someclass::function() const; // The method "function" promises to not
change any attributes of "someclass".

To get the maximum:

class A
{
public:
A();
~A();
const int * const getDataPointer() const
{
static const int test;
return &test;
}
};

Method "getDataPointer" is returning a const pointer to an int with const
content and not changing members of class A.

Loading...