const_cast

C

Comp1597

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
 
C

Comp1597

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?
 
N

Neelesh

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.
 
J

James Kanze

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*.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top