double x = 0char d = '0' + i
(
target-type)
value
int i = (int) 1.0;
conversion-kind<target-type>(source-value)
.
int i = static_cast<int>(1.0);
This is way ugly syntax , but logical within C++.
const
and volatile
.
*
and void *
, int
and double
, and so
on.
static_cast<
target-type>(val)
val
.
// expanding int i = static_cast<int>('c'); // truncating char c = static_cast<char>(1024); // transforming string s = static_cast<string>("hello");
You need to understand how the values are changed.
const
or volatile
modifiers.
volatile
is a hint to control compiler optimization.
const_cast<
target-type>(val)
from val type | to target-type |
const T * | T * |
const T & | T & |
const T | T & |
const int ci = 1; const int * cip = & i; const int & cir = i;int * ip = const_cast<int *>(ci);ip = const_cast<int *>(cip)ip = const_cast<int *>(cir);int & ir1 = const_cast<int &>(ci);int & ir2 = const_cast<int &>(cip);int & ir3 = const_cast<int &>(cir);
val
.
val
is in a read-only memory segment, the const cast is
useless.
void t(const int * ip, const string & sr, const vector<int> iv) { *(const_cast<int *>(ip)) = 3; const_cast<string &>(sr) = "hello"; (const_cast<vector<int> &>(iv))[0] = 0; }
These kinds of constant casts are almost always bad.
bool C::find(int value) const { const_cast<C *>(this)->find_count++; return fnd(value); }
reinterpret_cast<
target-type>(val)
val
and target-type
are usually (but not always) reference or
pointer types.
int i = 1; int & ir = i; int * ip = &i; string & cr = reinterpret_cast<string &>(i); string * cp = reinterpret_cast<string *>(i); string & sr = reinterpret_cast<string &>(ir); string * sp = reinterpret_cast<string *>(ir); string & sr = reinterpret_cast<string &>(ip); string * sp = reinterpret_cast<string *>(ip);
// wrong, a static cast. int i = reinterpret_cast<int>(1.0); // wrong, a reinterpret cast. char * cp = static_cast<char *>(ip);
dynamic_cast<
target-class>(val)
val
should be a pointer or reference to a class instance.
target-class
should be a pointer or reference to a class type.
val
doesn't point to an instance of target-class, the
dynamic cast returns 0.
C
, casting values of other types to or from values
of type C
is often useful.
Given the class big_int
,
big_int bi = "1000000000000000000000000000";
string msg = string("128! = ") + bi;
Other types convert to and from big_int
s in the same way.
C::C(const T & tval)
big_int::big_int(const string & str) { ... } big_int bi("100000000000000000000000000");
bi = "10";
"10"
gets converted to a string, which gets converted to a big int, which
gets assigned by bi
.
explicit
keyword can prevent these problems.
explicit C::C(const T & tval);
explicit big_int::big_int(const string & str) { ... } big_int bi("10"); // Counts as explicit.bi = "20";// Wrong - no explicit call. bi = big_int("30"); // Ok - explicit call.
T
, define a class
operator that casts to T
.
C::operator
target-type ()
big_int::operator string () { ... }
cout << static_cast<string>(bi) << "\n";
explicit
.
string big_int::get_string(void) const { return ...; } cout << bi.get_string() << "\n";
This page last modified on 6 April 2004.