An expression can be explicitly converted or cast to type T
using dynamic_cast<T>
, static_cast<T>
, reinterpret_cast<T>
, or const_cast<T>
, depending on what type of cast is intended.
C++ also supports function-style cast notation, T(expr)
, and C-style cast notation, (T)expr
.
\\(
\\)
\\(
expression-list \\)
\\(
\\)
\\(
expression-list \\)
dynamic_cast
\\<
type-id \\>
\\(
expression \\)
static_cast
\\<
type-id \\>
\\(
expression \\)
reinterpret_cast
\\<
type-id \\>
\\(
expression \\)
const_cast
\\<
type-id \\>
\\(
expression \\)
\\(
type-id \\)
cast-expressionAll six cast notations have one thing in common:
dynamic_cast<Derived&>(base)
, yields an lvalue. Therefore, when you want to do something with the same object but treat it as a different type, you would cast to an lvalue reference type.static_cast<string&&>(s)
, yields an rvalue.(int)x
, yields a prvalue, which may be thought of as a copy of the value being cast, but with a different type from the original.