下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use the non-modifiable pointer descriptor — TPtrC《如何使用不可修改的指针描述符TPtrC》
How to use the non-modifiable pointer descriptor — TPtrC
Non-modifiable pointer descriptors are useful for referencing constant strings or data; for example, accessing strings built into ROM resident code, or passing a reference to data in RAM which must not be modified through that reference.
-
For text data, it is usual to construct a
TPtrCtype and allow the appropriate variant, either aTPtrC8or aTPtrC16to be selected at build time. -
For binary data, an explicit
TPtrC8is used. -
It is rare to use an explicit
TPtrC16.
Constructing a TPtrC
A non-modifiable pointer descriptor can be constructed in a number of ways:
-
any other descriptor.
-
another non-modifiable pointer descriptor.
-
a pointer into memory and specifying the length of the data.
-
a zero terminated string.
The following code fragment constructs a TPtrC to represent the data already represented by any other type of descriptor.
The source descriptor is a literal which is converted to descriptor type.
_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText); // buf1 is the existing descriptor
...
TPtrC ptr(buf1); // data in buf1 now accessible through ptr
The following code fragment constructs a TPtrC to represent the data already represented by another TPtrC.
_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText); // buf1 is the existing descriptor
...
TPtrC ptr1(buf1); // data in buf1 now accessible through ptr1
TPtrC ptr2(ptr1); // data also accessible through ptr2
Although rarely used (possibly in porting legacy 'C' code), the following code fragment defines a constant ‘C’ style non-Unicode string and then constructs the non-modifiable pointer descriptor for that string. The explicit 8 bit variant is used here. The descriptor is separate from the data it represents.
const TText8* cstr = (TText8*)"Hello World!";
...
TPtrC8 ptr(cstr);
...
ptr.Length(); // The length is 12.
ptr.Ptr(); // The address of the descriptor's data,
// i.e. 'C' string.
The following code fragment shows construction from a pointer into memory and a length. The example assumes that both the pointer and the length have valid values:
TUint8* memptr;
TInt length;
...
TPtrC8 ptr(memptr,length);
Once a non-modifiable pointer descriptor has been constructed, the functions in the base class, TDesC, are available to access the data.
For example, given a pointer descriptor labelled ptr:#
_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText); // buf1 is the existing descriptor
...
TPtrC ptr(buf1); // data in buf1 now accessible through ptr
...
ptr.Length(); // returns the length of the data (i.e. 12)
本文介绍了Symbian系统中TPtrC描述符的多种构造方法及其用途,包括引用常量字符串或数据、构建从其他描述符、内存指针及长度等,并展示了如何通过这些描述符访问数据。
506

被折叠的 条评论
为什么被折叠?



