Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question: "const" infront and after a function-declaration: Meaning ?

37 views
Skip to first unread message

R.Wieser

unread,
Apr 15, 2013, 7:59:22 AM4/15/13
to
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

unread,
Apr 15, 2013, 8:55:03 AM4/15/13
to
>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

unread,
Apr 15, 2013, 12:16:29 PM4/15/13
to
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
David Lowndes <Dav...@example.invalid> schreef in berichtnieuws
63unm8hovfkjor1v6...@4ax.com...

David Lowndes

unread,
Apr 15, 2013, 12:27:11 PM4/15/13
to
>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.

>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. :)

>Hmmm ... I could imagine a function declared as 'const const int* result
>functionname();'

Did you mean to write "const const"?

>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).

>...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 Webber

unread,
Apr 15, 2013, 1:05:32 PM4/15/13
to
"R.Wieser" wrote in message
news:516beb44$0$2576$e4fe...@news2.news.xs4all.nl...
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

unread,
Apr 15, 2013, 4:49:59 PM4/15/13
to
Hello David,

> 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.

> 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 ?

> 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.

> 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


-- Origional message:
David Lowndes <Dav...@example.invalid> schreef in berichtnieuws
9v9om85gb67g8otn0...@4ax.com...

R.Wieser

unread,
Apr 15, 2013, 5:06:17 PM4/15/13
to
Hello David W,

> 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 ?

> 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.


-- Origional message:
David Webber <da...@musical.demon.co.uk> schreef in berichtnieuws
zrWat.386$pP5...@fx24.fr7...

David Lowndes

unread,
Apr 15, 2013, 5:47:08 PM4/15/13
to
>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.

> 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.

>> 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

David Webber

unread,
Apr 15, 2013, 5:53:45 PM4/15/13
to


"R.Wieser" wrote in message
news:516c6b73$0$2632$e4fe...@news2.news.xs4all.nl...

> 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.

>> 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.

R.Wieser

unread,
Apr 16, 2013, 9:33:14 AM4/16/13
to
Hello David L,

> 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 ...

> See these explanations:
[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


-- Origional message:
David Lowndes <Dav...@example.invalid> schreef in berichtnieuws
vtsom8l84dvhu1fls...@4ax.com...

R.Wieser

unread,
Apr 16, 2013, 10:01:20 AM4/16/13
to
Hello David W,

> 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.

> 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.

> 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.

> 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 ?

> 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


-- Origional message:
David Webber <da...@musical.demon.co.uk> schreef in berichtnieuws
NF_at.1347$Wa2...@fx04.fr7...

David Webber

unread,
Apr 16, 2013, 11:19:01 AM4/16/13
to


"R.Wieser" wrote in message
news:516d5959$0$2563$e4fe...@news2.news.xs4all.nl...

> 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

R.Wieser

unread,
Apr 16, 2013, 6:38:44 PM4/16/13
to
Hello David W,

> 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.

> (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


-- Origional message:
David Webber <da...@musical.demon.co.uk> schreef in berichtnieuws
NZdbt.819$Tr3...@fx13.fr7...

Christian Freund

unread,
Jun 19, 2013, 4:01:52 AM6/19/13
to
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.

0 new messages