×
Namespaces

Variants
Actions

Typecasting in Symbian

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: vasant21 (09 May 2007)
Last edited: hamishwillee (21 Oct 2011)
Casting is always dangerous and should be used with caution, situation demanding cast generally reflects design weakness. Casting is one well known source of hard-to-find errors.


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

1) static_cast

2) const_cast

3) reinterpret_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

1) static_cast

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.


Example :

TInt Value;
TBool iAvailable = static_cast<TBool>(Value);

2) const_cast

const_cast is used to remove constness.

Example :

const TInt a = 10;
const TInt* b = &a;
TInt* c = const_cast<TInt*>(b);


3) reinterpret_cast

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.

Example :

CAknWaitNoteWrapper* waitNoteWrapper = CAknWaitNoteWrapper::NewL();
 
// reinterpret_cast is required as CAknWaitNoteWrapper inherits privately from CActive! //
 
CleanupStack::PushL(reinterpret_cast<CBase*>(waitNoteWrapper));


4) dynamic_cast

As Run Time Type Information (RTTI) is not supported, dynamic casting ( dynamic_cast<type>(exp) ) should not be used in symbian.


Tips

1) Use explicit typecasts to clarify your code and to avoid problems caused by unexpected implicit casts, such as

integral promotions.

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.

This page was last modified on 21 October 2011, at 06:10.
55 page views in the last 30 days.
×