Sunday, September 28, 2008

How TDesC and TLitC (generated using _LIT macro) class are related ?



When i started working on Symbian-C++ (i was also new to C++) , i was wondering how the below code is compiling.

This was not trivial as i looked at class hierarchy of TDesC.

Reproduced from this book.

TLitC is not present in TDesC class hierarchy.
(If TLitC was there in the hierarchy, then derived class object(TLitC) can be passed where Base Class object(TDesC) is expected.)

How TLitC class object is converted to TDesC ?

Then i started looking into Symbian books and one of the book came close to the answer, but could not convince fully. Here is an excerpt from the book (I missed the code snippet in the book which was present above this excerpt.)

"TLitC16 (and TLitC8) do not derive from TDesC8 or TDesC16 but they have the same binary layouts as TBufC8 or TBufC16. This allows objects of these types to be used wherever TDesC is used."

This seems to be logically correct.
But how will the compiler know, the two class have same binary layout ?
Does the compiler have that intelligence ?
So i tried the below program


Both 'X' and 'Y' class have same binary layout, but the compiler gives following error

"error C2664: 'PrintObj' : cannot convert parameter 1 from 'class X' to 'class Y &' "

At that time, I was parallely reading Stroustrup's book "Design and Evolution of C++".
In chapter 3 of the book, "conversion operator" is introduced, which is the concept behind TLitC to TDesC conversion.

I will briefly describe the "conversion operator" here.
Going back to "class X" and "class Y" example, we need to add "class X " to "class Y" conversion operator, so that "class X" objects can be passed to PrintObj function(which expects class Y objects).Adding "operator const Y& () const" to "class X", objects of type "class X" can be converted to converted to "class Y".

Similarly in case of TLitC, following conversion operator converts TLitC to TDesC.


In summary, "Conversion Operator" does the trick of converting TLitC to TDesC.

Sunday, September 21, 2008

Kickoff

Welcome to my webblog.

My aim is to share information(hopefully useful !!) on mobile phone software development, mostly on Symbian-C++(native language used in Symbian OS) .

Also, i plan to highlight some of the mobile phone features, which are common to all platforms.

U can subscribe to my posts, through RSS (Find it on top right corner)

No idea about RSS?
See this video (Lee LeFever of commoncraft show, explains in plain English)