How to use the non-modifiable pointer descriptor — TPtrC

本文介绍了Symbian系统中TPtrC描述符的多种构造方法及其用途,包括引用常量字符串或数据、构建从其他描述符、内存指针及长度等,并展示了如何通过这些描述符访问数据。

下面的内容摘自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 TPtrC type and allow the appropriate variant, either a TPtrC8 or a TPtrC16 to be selected at build time.

  • For binary data, an explicit TPtrC8 is 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);

[Top]


Accessing data

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)


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值