Casting

A

Asapi

"Only static_cast can be used with value types; const_cast and dynamic_cast
can only be used with pointers and references."

Is this a correct statement?

Can reinterpret_cast be used with references at all?

const_cast casts away constantness. Can const_cast be used to cast back
constantness if possible at all?

Thanks!
 
S

Simon Saunders

"Only static_cast can be used with value types; const_cast and dynamic_cast
can only be used with pointers and references."

Is this a correct statement?

Yes.

Can reinterpret_cast be used with references at all?

Yes.

const_cast casts away constantness. Can const_cast be used to cast back
constantness if possible at all?

There is no need to "cast back constantness"; non-const pointers and
references convert implicitly to const ones, e.g.:

int x = 5;
int* y = &x;
const int* z = y;
 
J

Jerry Coffin

"Only static_cast can be used with value types; const_cast and dynamic_cast
can only be used with pointers and references."

Is this a correct statement?

It's half right. he part after the semicolon is correct, but the part
preceding it is wrong. In addition to static_cast, you can use either a
c-style cast or a ctor-style cast with a value type.
Can reinterpret_cast be used with references at all?

Yes, though using the result is usually non-portable.
const_cast casts away constantness. Can const_cast be used to cast back
constantness if possible at all?

You don't normally need an explicit cast to go from 'X' to 'X const'.
Also note that const_cast can be used to cast away volatility.
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top