Typecasting in Symbian
C++ casting operators were not supported prior to Symbian OS v6.0, Instead symbian used macros like : REINTERPRET_CAST, STATIC_CAST, CONST_CAST, MUTABLE_CAST.
Types of Cast
4) dynamic_cast - Not Available.
From Symbian OS v6.0 onwards gcc fully supports the C++ casting operators, and the macros have been changed to reflect this in Symbian OS v6.1. This means that from Symbian OS v6.0 onwards, developers should use the native C++ casting operators rather than the macros.
Which cast to use
static_cast performs safe and relatively portable casts. For example, you use static_cast to explicitly document a cast that would otherwise take place automatically.
Symbian C++ automatically casts TBool to TInt in this context so the use of static_cast in this case isn't necessary. However, by using it, programmers document their intention explicitly. The use of static_cast enables the compiler to catch programmers mistakes.
TBool iAvailable = static_cast<TBool>(Value);
const_cast is used to remove constness.
const TInt a = 10;
const TInt* b = &a;
TInt* c = const_cast<TInt*>(b);
As opposed to static_cast, reinterpret_cast performs relatively dangerous and nonportable casts. Typecasting between any incompatible type must be done with a reinterpret_cast(type,exp) macro.
CAknWaitNoteWrapper* waitNoteWrapper = CAknWaitNoteWrapper::NewL();
// reinterpret_cast is required as CAknWaitNoteWrapper inherits privately from CActive! //
As Run Time Type Information (RTTI) is not supported, dynamic casting ( dynamic_cast<type>(exp) ) should not be used in symbian.
1) Use explicit typecasts to clarify your code and to avoid problems caused by unexpected implicit casts, such as
2) Avoid casting to any type that has a different size than the original.
3) Avoid removing a const attribute.
4) Use a separate cast for constness and representation. In case a static_cast or a reinterpret_cast to a
non-const type is used, whose argument is a const object of an otherwise appropriate type, use an additional
const_cast to the argument in question.
5) Typecast from a derived class to a base class is, in most cases, implicit. If an explicit cast is needed or if you
need to cast from a base class to a derived class, use static_cast<type>(exp).
6) Typecasting between any other incompatible type must be done with a reinterpret_cast(type,exp) macro.
7) One should never break four byte alignment with any typecast.