This page was last modified 07:30, 25 June 2008.
Typecasting in Symbian
From Forum Nokia Wiki
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.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is series 60 SDK supporting Symbian 7.0 available ? | maarora | Symbian Tools & SDKs | 9 | 2003-10-06 11:08 |
| symbian os c++ | yuvajeeva | General Symbian C++ | 1 | 2004-01-08 09:10 |
| 3rd上的资源问题 | xu_caveman | Symbian | 22 | 2008-07-01 03:50 |
| Starting with Symbian with Java | smar | Mobile Java General | 0 | 2008-01-16 00:23 |
| How to integrate symbian in j2me | Basu | Mobile Java General | 1 | 2007-12-06 04:36 |
