Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const_cast

Reply
Thread Tools

const_cast

 
 
Comp1597@yahoo.co.uk
Guest
Posts: n/a
 
      05-03-2009
I've been told that const_cast "adds or removes the constness of an
expression".
I understand that it converts pointers to const to pointers to non-
const and references to const to references to non-const. But how
does a const_cast affect a variable of type const int* const? I would
assume a const_cast would make such a variable have type const int*
Is this correct?

Thanks
 
Reply With Quote
 
 
 
 
Comp1597@yahoo.co.uk
Guest
Posts: n/a
 
      05-03-2009
On May 3, 9:36*pm, (E-Mail Removed) wrote:
> I've been told that const_cast "adds or removes the constness of an
> expression".
> I understand that it converts pointers to const to pointers to non-
> const and references to const to references to non-const. *But how
> does a const_cast affect a variable of type const int* const? *I would
> assume a const_cast would make such a variable have type const int*
> Is this correct?
>
> Thanks


Sorrmy I meant that const_cast transforms the type const int* const to
int* const. Is this correct?
 
Reply With Quote
 
 
 
 
Neelesh
Guest
Posts: n/a
 
      05-04-2009
On May 4, 1:17*am, (E-Mail Removed) wrote:
> On May 3, 9:36*pm, (E-Mail Removed) wrote:
>
> > I've been told that const_cast "adds or removes the constness of an
> > expression".
> > I understand that it converts pointers to const to pointers to non-
> > const and references to const to references to non-const. *But how
> > does a const_cast affect a variable of type const int* const? *I would
> > assume a const_cast would make such a variable have type const int*
> > Is this correct?

>
> > Thanks

>
> Sorrmy I meant that const_cast transforms the type const int* const to
> int* const. *Is this correct?


The following const_cast will transform a variable of type const int*
const to int* const
(It removes constness of the data).
const int i = 10;
const int* const r = &i;
int* const r2 = const_cast<int* const>(r);

However, since the actual data (i) is originally cosnt, any attempt to
change the value of i through r2 will result in undefined behavior.


The following const_cast will tranfrom a variable of type const int*
const to const int*
(It removes constness of the pointer)

const int i = 10;
const int* const r = &i;
const int* r3 = const_cast<const int*> (r);

Note that the const_cast is actually redundant here, since we are
removing "top level" constness for a pointer, which is anyway legal as
we are doing a copy. Had r3 been a reference, then you would need an
explicit const_cast:

const int*& r3 = r; //Error
const int*& r3 = const_cast<const int*&> (r); //Works.





 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-04-2009
On May 3, 9:36 pm, (E-Mail Removed) wrote:
> I've been told that const_cast "adds or removes the constness of an
> expression".
> I understand that it converts pointers to const to pointers to
> non- const and references to const to references to non-const.
> But how does a const_cast affect a variable of type const int*
> const? I would assume a const_cast would make such a variable
> have type const int* Is this correct?


Maybe. It depends on the cast itself. Note however that the
results of a const_cast (or any cast not returning a reference
type) is an rvalue, so top level const has no meaning. (It
would have meaning if the results were a class type, but
const_cast can never return a class type.) So even if you
const_cast< int const* const >, the results will in fact be int
const*.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const_cast question Gajanan Bhat C++ 9 05-05-2004 12:29 PM
const_cast S.Senthilvel C++ 4 01-08-2004 06:59 PM
const_cast<> R. Anbeeswaran C++ 7 11-14-2003 12:15 PM
const_cast question drowned C++ 3 08-04-2003 08:51 AM
[help] const_cast Kaspar Minosiants C++ 2 07-21-2003 12:43 PM



Advertisments