The "const" modifier

D

Der Andere

What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn't copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?

Cheers,
Matthias
 
T

Thomas Matthews

Der said:
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn't copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?

Cheers,
Matthias

These issues are discussed in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6
http://www.parashift.com/c++-faq-lite/const-correctness.html

Searching the FAQ before posting is always an excellent idea.
One should also search the newsgroups too.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Kevin Goodsell

Der said:
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn't copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?

It means the returned temporary object is const. I posted a thread about
this a while back:

http://groups.google.com/[email protected]

Unfortunately it didn't generate much discussion. That thread was
prompted by GotW #6, here:

http://www.gotw.ca/gotw/006.htm

Which may be enlightening for you.

-Kevin
 
D

Derek

Der said:
What exactly is the sense of using a "const" modifier
before a function declaration? Actually, you can use
it in three places. For instance, take a look at the
following function declaration (from the introductory
book "Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn't copy the whole code (there are many more member
functions), but this should suffice for the question.

The meaning of the "const" before the parameter "amount2"
is obvious to me; the "const" just before the semicolon
signals that the function may not change the calling
object if I got it right. But what about the first
"const"?

It means the Money temporary returned by operator+ is
const. If that const was not there, the compiler would
merrily accept this (if op+= was defined, of course):

Money a, b, c, d;
a = (b + c) += d;

With the const, the compiler would complain. The argument
made for returning const objects is to prevent cases like
that above, because you can't write (b+c)+=d for built-in
types. The idea is that making classes behave more like
build-in types is better. I don't much see the point. If
the user wants to write (b+c)+=d, I say let 'em, even if it
wouldn't work with int or double.
 
H

Howard

Derek said:
... The idea is that making classes behave more like
build-in types is better. I don't much see the point. If
the user wants to write (b+c)+=d, I say let 'em, even if it
wouldn't work with int or double.

And I say, if they *do* write code like that...shoot 'em! :)

Honestly, I think the fact that C++ allows so many varied and complex ways
of combining operations into one line of code has caused more maintenance
headaches than they're worth. I love C++, but I almost always put one and
only one operation on a line of code, and let the optimizer reduce it if
wants to. I want my code to readable (and debuggable). Better three lines
of readable code than one line that requires three hours of research to
decipher. Maybe it's just the old Pascal programmer in me, but that's my
opinion anyway.

(Of course, if it's job security you're after, maybe writing indecipherable
code is a *good* thing...?)

-Howard
 
D

Der Andere

class Money
It means the Money temporary returned by operator+ is
const. If that const was not there, the compiler would
merrily accept this (if op+= was defined, of course):

Money a, b, c, d;
a = (b + c) += d;

With the const, the compiler would complain. The argument
made for returning const objects is to prevent cases like
that above, because you can't write (b+c)+=d for built-in
types. The idea is that making classes behave more like
build-in types is better. I don't much see the point. If
the user wants to write (b+c)+=d, I say let 'em, even if it
wouldn't work with int or double.

Got the point :)
If I'd use const everywhere I _should_ do, the code would probably get
awkward beyond recognition. I won't use the first const anyway ... I trust
in myself to not produce such a degree of non-sensical code, so I agree with
you.

Regards,
Matthias
 
D

Dave Moore

*snip*
If I'd use const everywhere I _should_ do, the code would probably get
awkward beyond recognition.

*Sigh* You wouldn't feel this way if you'd taken the time to read (and
think about and try to understand)
1) the FAQ links
(http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6
http://www.parashift.com/c++-faq-lite/const-correctness.html), or

2) the Guru-of-the-week entry (http://www.gotw.ca/gotw/006.htm),

as suggested by other posters in this thread. Proper use of const is
absolutely essential for proper, readable, debuggable C++ code. In
particular, the distinction between returning a const & or non-const &
can be extremely important for designing certain types of classes
(c.f. Stroustrup's TC++PL section 5.5).
I won't use the first const anyway ... I trust in myself to not produce such > a degree of non-sensical code

Wow! You have demonstrated don't even understand proper use or even
the potential benefits of the const qualifier yet .. which makes your
statement above highly dubious. Perhaps you would really would never
construct such and artificially awkward mathematical expression, but
there are myriad other ways to generate "non-sensical code". If you
are serious about programming in C++, you should really reconsider
your (by inference) cavalier attitude towards essential language
features like the const qualifier.
 
D

Derek

Der said:
Got the point :) If I'd use const everywhere I
_should_ do, the code would probably get awkward beyond
recognition. I won't use the first const anyway ...
I trust in myself to not produce such a degree of
non-sensical code, so I agree with you.

Const correctness is important. MOST of the time I use
const wherever I can. There are very few cases where I
don't use const when I can. You example just happens
to be one of the cases where I choose not to:

const AnotherClass operator+(...); // I wouldn't use
// const here

There are other examples, but not many. SOMETIMES it's
better to use iterator instead of const_iterator (see
"Effective STL" by Scott Meyers), and I don't usually
bother to const local variables:

int x = (a + b) / c * d; // could declare x const,
// but I usually don't

But in all of the other common cases I use const very
rigorously (e.g., passing arguments by reference to
const, member functions, constants, etc.)

What I'm trying to stress is that const is a Good Thing
and you should use it wherever possible (and practical).
There are cases where const can be disregarded, but
think of them as exceptions to the rule.
 
D

David Riebenbauer

* Daniel T. said:
[...]
These issues are discussed in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6
http://www.parashift.com/c++-faq-lite/const-correctness.html

Searching the FAQ before posting is always an excellent idea.
One should also search the newsgroups too.

I don't see where his question is answered in the FAQ...

You're right. I also didn't find it.
But as this postin ointed me to some usefull knowledge about const it
wasn't totally pointless.

David
 
S

Stephen Sprunk

Der Andere said:
Got the point :)
If I'd use const everywhere I _should_ do, the code would probably get
awkward beyond recognition. I won't use the first const anyway ... I trust
in myself to not produce such a degree of non-sensical code, so I agree with
you.

const allows the compiler to optimize code in ways that may not be obvious,
and it can prevent bugs by refusing to compile code that appears correct but
is either incorrect or just nonsensical.

S
 
P

Peter van Merkerk

Stephen said:
const allows the compiler to optimize code in ways that may not be
obvious,

It is questionable if it really helps optimization
(http://www.gotw.ca/gotw/081.htm). But...
and it can prevent bugs by refusing to compile code that
appears correct but is either incorrect or just nonsensical.

....is as far as I am concerned the most important reason for using const.
Documenting your intentions to whoever has to use and/or maintain the code
(which may very well be you in few months) being a close second.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top