Const'ness


Subject: Const'ness
From: Thomas Fletcher (thomasf@qnx.com)
Date: Fri Jul 07 2000 - 12:23:46 CDT


On Fri, 7 Jul 2000, Martin Sevior wrote:
> On Fri, 7 Jul 2000, Thomas Fletcher wrote:
> >
> > Martin,
> >
> > I just patched in some code to cast away a const in fl_AutoNum.cpp
> > that you committed. I thought it might be list related (but in
> > retrospect now I don't think so). In any case you might want to
> > check and see that casting this const away is valid, otherwise
> > we should change the code (getType) to return a const pointer
> > and not a plain pointer. People please take care in this regard.
> > const is there for a reason ... if you are just going to do
> > away with it, then don't use it in the first place!
> >
>
> Hi Thomas,
> Thanks for clearing up the compiler warning. The code still
> works. Does it compile in QNX?
>
> Can you enlighten me as when one would want to return a const pointer
> instead of a regular pointer? I've had no formal C or C++ training.

Well I profess to be no expert, but in general if
you are returning a const pointer you are indicating
to the calling program that they can't do any modification
of the data that this pointer points to. This allows
the compiler to do some extra optimization, along with
enforcing better programming habits (which is why we
are catching there errors when we mess something
up.

The most excellent reference that I can point you to
is the Stroustrop book section 5.4 which discusses
all sorts of differences between const's and the
ramifications of each.

The main worry that I have with casting away const
is that the const behaviour might have been counted
on at some point by a higher level app.

For more info:
http://www.cerfnet.com/~mpcline/c++-faq-lite/const-correctness.html

Thomas
-------------------------------------------------------------
Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com Neutrino Development Group
(613)-591-0931 http://www.qnx.com/~thomasf



This archive was generated by hypermail 2b25 : Fri Jul 07 2000 - 12:23:59 CDT