The easiest way to learn about the descriptor is trough the equivalent code in C. In this post, I will write a short comparison, this article is inspired by the explanation about Symbian Descriptor on the the Professional Symbian Programming
(I said inspired, my explanation is different, and the code in the copy of the book that I have is sometimes incorrect). In this part I will talk only about constant/immutable strings.
In C, if you define something like this:
static const char hellorom[] = "hello";
Then the string “hello” will be on a non-modifyable memory space (may be in ROM or RAM, but it can not be modified).
The equivalent code in Symbian is:
_LIT(KHelloROM, "hello");
Everything is still easy to understand here: you can not modify the contents of KHelloROM or hellorom. Something to note is that in Symbian the string is not NULL terminated, the length of the string is recorded in KHelloROM.
Now, in C, you can also have a pointer to the same location:
const char *helloptr = hellorom;
We can have that in Symbian:
TPtrC helloPtr(KHelloROM);
helloPtr contains a pointer to KHelloROM, and the length of the descriptor pointed by that pointer. You may think it is a bit strange: why do we need to store the length again?. The reason is that we can have a pointer to the first n part of the string, like this:
TPtrC ptr2(KHelloROM().Ptr(), 2);
If you try to display/print ptr2, it will say the string “he”.
Continue reading »