Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   const_cast (http://www.velocityreviews.com/forums/t682444-const_cast.html)

Comp1597@yahoo.co.uk 05-03-2009 07:36 PM

const_cast
 
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

Comp1597@yahoo.co.uk 05-03-2009 08:17 PM

Re: const_cast
 
On May 3, 9:36*pm, Comp1...@yahoo.co.uk 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?

Neelesh 05-04-2009 04:23 AM

Re: const_cast
 
On May 4, 1:17*am, Comp1...@yahoo.co.uk wrote:
> On May 3, 9:36*pm, Comp1...@yahoo.co.uk 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.






James Kanze 05-04-2009 08:20 AM

Re: const_cast
 
On May 3, 9:36 pm, Comp1...@yahoo.co.uk 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:james.kanze@gmail.com
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


All times are GMT. The time now is 03:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.