Sunday, October 30, 2011

Type Casting in C++

There are several ways to cast one type to another in C++. I'd like to categorize them as implicit conversion, explicit conversion, and keyword conversion (which is also explicit, and I separate it for discussion).

Implicit Conversion

As in C, some casting can be automatically done for some compatible types.
int a = 100;
double b;
b = a;
Also, such implicit conversion occurs when initialize or assign to class objects, calling the overloaded "=" operator or constructor.
class A {};
class B { public: B(A a){} };
A a;
B b = a;

Explicit Conversion
Like in C, we can use parentheses to cast types. The ways for explicit casting include:
(new_type) expression
new_type (expression)
(new_type) (expression)
Some notes for this:
  1. At least one pair of parentheses is required.
  2. If the new type is pointer, reference, or contains multiple words (like const char), the parentheses for new_type is required.
  3. If the expression is not a single variable or number (like a+b, the parenthesis for expression is required.
  4. This explicit casting applies to almost any types (including class types with explicit constructor or operator function), but there may be errors in run time.

Keyword Conversion
Four keywords are available for casting: static_cast, dynamic_cast, reinterpret_cast, const_cast. The usage for keyword conversion is
static_cast<new_type>(expression)
dynamic_cast<new_type>(expression)
reinterpret_cast<new_type>(expression)
const_cast<new_type>(expression)
Note: the parentheses for expression are always required, even when the expression is a single variable or number.

static_cast
  1. It can be used to cast between fundamental types, as well as class types for those with explicit constructors or operator functions.
  2. It can perform casting between pointers or references to related classes. The compiler won't complain if casting base class pointer to derived class pointer. The overhead of type-safe check by dynamic_cast can be avoided. It's the programmers' responsibility to ensure safe conversions.
dynamic_cast
  1. It can be used only with pointers or references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.
  2. Dynamic casting from derived class to base class is always successful, while from base class to derived class, successful only when the base class is polymorphic.
  3. It requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly.
reinterpret_cast
  1. It converts any pointer type to any other pointer type, even of unrelated classes.
  2. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked, and thus dereferencing it is unsafe..
  3. It can also cast pointers to or from integer types.
const_cast
  1. It manipulates the constness or volatileness of an object, either to be set or to be removed.

References
C++ Tutorial

Related Posts:
Type Casting on Classes
static_cast, dynamic_cast, and reinterpret_cast
Assignment between Class Objects

No comments:

Post a Comment