Re: How to cast from const to non-const

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Liviu Andron (landron_at_bitdefender.com)
Date: 11/23/04


Date: Tue, 23 Nov 2004 19:51:47 +0200


"Liviu Andron" <landron@bitdefender.com> wrote in message
news:#Ial$PX0EHA.3596@TK2MSFTNGP12.phx.gbl...
> "Simon Trew" <noneofyour@business.guv> wrote in message
> news:#Tmts2I0EHA.1524@TK2MSFTNGP09.phx.gbl...
> > "Mike Gleason Jr Couturier" <mikejrMAPSON@videotron.ca> wrote in message
> > news:%2310O4nnzEHA.2012@TK2MSFTNGP15.phx.gbl...
> > > First I totally agree that one should never have
> > > to use const_cast.
> > >
> > > But doesn't const checkings are only at compile time ?
> > > Why would that produce undefined behavior then ? Why
> > > would const_cast exists ?
> > >
> >
> > const qualification affects the mangled (decorated) signature of the
> > function in the linker, so it's not quite purely a compile-time thing.
But
> > yes, the constness rules are in general enforced by the compiler.
> >
> > The problem is that if you want your code to be rigorously const
correct,
> > every API that it uses must also be const correct. If you're dealing
with
> > 3rd party APIs and technologies this may not be the case. (For example,
> COM
> > doesn't have the notion of const.) const_cast was introduced explicitly
> to
> > enable constness (and only constness) to be cast away when an API wants
a
> > non-const pointer/reference but your own code has a const one. However,
as
> a
> > programmer you have to be sure that the API really doesn't modify the
> data,
> > for this to be safe.
> >
> > It is also fairly common to use a data structure to pass data between an
> app
> > and both "getter" and "setter" functions. For example, ListView_SetItem
> and
> > ListView_GetItem both take a LVITEM structure. In this structure, there
is
> a
> > pointer to the string of text, LPTSTR pszText. The getter method will
fill
> > this in, but the setter item will use whatever you supply. You'd like to
> be
> > able to supply a const version of LPTSTR, i.e. LPCTSTR, but you can't of
> > course. So const_cast comes in quite handy in these cases, again, when
you
> > know the API doesn't actually modify the data.
> >
> > Another use is to get a non-const version of the "this" pointer in a
const
> > member function, for example, where a member function that is logically
> > const actually mutates the object's member data, e.g. by cacheing
> > information. It can often be undesirable to remove the constness of the
> > member function. However, in this case it's usually better to use the
> > mutable keyword on the member data declaration.
> >
> >
>
> Another use (when you don't control received parameters) ?
>
> struct MyStruct{
> ...
> TCHAR path[MAX_PATH];
> ....
> };
>
> bool func( const MyStruct &st)
> {
> ...
> (const_cast<MyStruct&>st).path[MAX_PATH - 1] = _T('\0');
> ...
> }
>
>

and another ( it's actually a 'hack', but...): a string fragment (char* +
size) when calling a function that receives a null-terminated string ( save
the last character, force a const_cast, use the function, restore the last
character)



Relevant Pages

  • Re: How to cast from const to non-const
    ... the constness rules are in general enforced by the compiler. ... > The problem is that if you want your code to be rigorously const correct, ... > every API that it uses must also be const correct. ... > member function, for example, where a member function that is logically ...
    (microsoft.public.vc.language)
  • Re: How to cast from const to non-const
    ... const qualification affects the mangled signature of the ... the constness rules are in general enforced by the compiler. ... every API that it uses must also be const correct. ... member function, for example, where a member function that is logically ...
    (microsoft.public.vc.language)
  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • C++ errors
    ... from `const void*' to ` ... member function `static ... char*, unsigned int, const ...
    (Fedora)
  • Re: const class member function
    ... >> Declaring a member function with the 'const' keyword ... > Is this "const" keyword info for function caller or it is ... invoked only for nonconst objects. ...
    (microsoft.public.vc.language)