Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

const_cast, reinterpret_cast

 
 
Unforgiven
Guest
Posts: n/a
 
      06-24-2004
"johny smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can someone please give me some good reference site/information for
> understanding when and why I would use
>
> 1.) const_cast
> Don't know why you would do this?
>
> 2.) reinterpret_cast.
> I have used this a little when I converted a primitive to a class, but
> would like to see other applications.


This is a very good article about reinterpret_cast:
http://msdn.microsoft.com/library/en...ep06012000.asp

Don't worry about the fact that it's on MSDN, it's not in the least
Microsoft specific. Robert Schmidt (the author) is a very standards-oriented
guy.
There's an article about static_cast too, but not about const_cast,
unfortunately.

--
Unforgiven

 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      06-24-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:2jvbacF15ln3kU1@uni-

> class MyIntContainer
> {
> iterator find(int x)
> {
> // some complex function
> }
>
> const_iterator find(int x) const
> {
> return const_cast<MyIntContainer*>(this)->find(x);
> }


Coming to think of it, shouldn't you implement the const version, and
nonconst one in terms of the const one. Because the original object may
have been const. I didn't realize this until formulating my reply to Mats
Weber.


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      06-24-2004
"Mats Weber" <(E-Mail Removed)> wrote in message news:matsw-
> "Siemel Naran" <(E-Mail Removed)> wrote:


> >If you implement the const function, you can implement the nonconst

function
> >in terms of the nonconst one.
> >
> >const T& Foo::stuff() const {
> > /* lots of code */
> >}
> >
> >T& Foo::stuff() {
> > const Foo * cthis = this;
> > return const_cast<T&>(cthis->stuff);
> >}

>
> Is there a clean (without a cast) way to do this, implementing the
> non-const version first ?


No. In this second approach example you need a const_cast to cast away the
constness of this. So either way you need a const_cast.

const T& Foo::stuff() const {
Foo * ncthis = const_cast<Foo *>(this);
return ncthis->stuff();
}

The advantage of the first way I posted is that the original object may have
been declared const as in

int main() {
const Foo foo;
foo.stuff();
}

so it's not safe to cast away the constness of this. So we implement the
const version. The nonconst version will call the const version, then cast
away the constness of the returned reference or iterator or pointer -- which
should be OK because the original pointer, presumably a pointer to some part
of the object, was originally non-const.



 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      06-24-2004
"Unforgiven" <(E-Mail Removed)> wrote in message
news:2jvvmsF13jrcmU1@uni-

> This is a very good article about reinterpret_cast:
> http://msdn.microsoft.com/library/en...ep06012000.asp


Good article!


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-24-2004
Siemel Naran wrote in
news:3eDCc.128476$(E-Mail Removed) in
comp.lang.c++:

> "John Harrison" <(E-Mail Removed)> wrote in message
> news:2jvbacF15ln3kU1@uni-
>
>> class MyIntContainer
>> {
>> iterator find(int x)
>> {
>> // some complex function
>> }
>>
>> const_iterator find(int x) const
>> {
>> return const_cast<MyIntContainer*>(this)->find(x);
>> }

>
> Coming to think of it, shouldn't you implement the const version, and
> nonconst one in terms of the const one. Because the original object
> may have been const. I didn't realize this until formulating my reply
> to Mats Weber.
>
>


Well should you be able to make an iterator from a const_iterator ?

Ofcourse calling the non-const find on a const object is fine as
long as we know that it doesen't modify the object, and in this
case why would it ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      06-24-2004
"Rob Williscroft" <(E-Mail Removed)> wrote in message

> Ofcourse calling the non-const find on a const object is fine as
> long as we know that it doesen't modify the object, and in this
> case why would it ?


Note in the example we are returning an iterator to the caller. There's no
saying what they'll do with it.

Also, the standard says casting away the constness of an object that was
originally const, even if only for read purposes, is undefined. Maybe some
implementations out there have read only pointers builtin into the system, I
don't know. I suggested recently in a post that to get a "char *" from
string.c_str() you could practically do "const_cast<char*>(string. c_str())"
which would work on all implementations I know, but someone correctly
pointed out it is still undefined behavior if the original string were
const.


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-24-2004
Siemel Naran wrote in
news:4NDCc.128652$(E-Mail Removed) in
comp.lang.c++:

> "Rob Williscroft" <(E-Mail Removed)> wrote in message
>
>> Ofcourse calling the non-const find on a const object is fine as
>> long as we know that it doesen't modify the object, and in this
>> case why would it ?

>
> Note in the example we are returning an iterator to the caller.
> There's no saying what they'll do with it.
>


I was refering to the const_cast in the const member function,
and then calling the non-const find on it.

> Also, the standard says casting away the constness of an object that
> was originally const, even if only for read purposes, is undefined.


Do you have a reference ?

> Maybe some implementations out there have read only pointers builtin
> into the system, I don't know. I suggested recently in a post that to
> get a "char *" from string.c_str() you could practically do
> "const_cast<char*>(string. c_str())" which would work on all
> implementations I know, but someone correctly pointed out it is still
> undefined behavior if the original string were const.
>


Its UB anyway, we've no idea where the char const * returned by
c_str() comes from.


Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      06-24-2004
>> class MyIntContainer
>> {
>> iterator find(int x)
>> {
>> // some complex function
>> }
>>
>> const_iterator find(int x) const
>> {
>> return const_cast<MyIntContainer*>(this)->find(x);
>> }

>
>Coming to think of it, shouldn't you implement the const version, and
>nonconst one in terms of the const one. Because the original object may
>have been const. I didn't realize this until formulating my reply to Mats
>Weber.


In this case you probably can't implement the non-const version in terms of the
const version.

The return type of the const version is most likely not convertible to the
return type of the non-const version.

It seems reasonble that one shouldn't be able to convert a constant iterator
into a non-constant iterator.

The reverse of course is quite reasonable.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-24-2004
"David White" <(E-Mail Removed)> wrote:
> "johny smith" <(E-Mail Removed)> wrote
> > 1.) const_cast
> > Don't know why you would do this?

>
> It's for casting from const to non-const.


Also for casting from non-const to const, and volatile to non-volatile,
and non-volatile to volatile, and any combinations of those.
For example:

void foo(const char **ptr);
char *x;

foo(const_cast<const char **>(&x));
 
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
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
reinterpret_cast problem John Harrison C++ 6 07-13-2004 11:39 PM
reinterpret_cast<> Aman C++ 15 02-25-2004 03:03 PM
reinterpret_cast<>() v. static_cast<>() Scott Brady Drummonds C++ 11 01-20-2004 09:12 PM
reinterpret_cast - to interpret double as long Suzanne Vogel C++ 17 07-07-2003 02:50 PM



Advertisments