Tutorial by Examples

A pointer to base class can be converted to a pointer to derived class using static_cast. static_cast does not do any run-time checking and can lead to undefined behaviour when the pointer does not actually point to the desired type. struct Base {}; struct Derived : Base {}; Derived d; Base* p1 ...
A pointer to a const object can be converted to a pointer to non-const object using the const_cast keyword. Here we use const_cast to call a function that is not const-correct. It only accepts a non-const char* argument even though it never writes through the pointer: void bad_strlen(char*); const...
A pointer (resp. reference) to an object type can be converted to a pointer (resp. reference) to any other object type using reinterpret_cast. This does not call any constructors or conversion functions. int x = 42; char* p = static_cast<char*>(&x); // error: static_cast cannot perf...
An object pointer (including void*) or function pointer can be converted to an integer type using reinterpret_cast. This will only compile if the destination type is long enough. The result is implementation-defined and typically yields the numeric address of the byte in memory that the pointer poin...
A conversion that involves calling an explicit constructor or conversion function can't be done implicitly. We can request that the conversion be done explicitly using static_cast. The meaning is the same as that of a direct initialization, except that the result is a temporary. class C { std:...
static_cast can perform any implicit conversion. This use of static_cast can occasionally be useful, such as in the following examples: When passing arguments to an ellipsis, the "expected" argument type is not statically known, so no implicit conversion will occur. const double x = ...
static_cast can convert from an integer or floating point type to an enumeration type (whether scoped or unscoped), and vice versa. It can also convert between enumeration types. The conversion from an unscoped enumeration type to an arithmetic type is an implicit conversion; it is possible, but ...
A pointer to member of derived class can be converted to a pointer to member of base class using static_cast. The types pointed to must match. If the operand is a null pointer to member value, the result is also a null pointer to member value. Otherwise, the conversion is only valid if the member ...
In C++, void* cannot be implicitly converted to T* where T is an object type. Instead, static_cast should be used to perform the conversion explicitly. If the operand actually points to a T object, the result points to that object. Otherwise, the result is unspecified. C++11 Even if the operand do...
C-Style casting can be considered 'Best effort' casting and is named so as it is the only cast which could be used in C. The syntax for this cast is (NewType)variable. Whenever this cast is used, it uses one of the following c++ casts (in order): const_cast<NewType>(variable) static_cast&...

Page 1 of 1