Monday, May 09, 2005
Tip 1: Never instantiate a TDes or a TDesC
The default constructors of TDes and TDesC are declared private so the compiler won’t let you construct them directly. But there is no copy constructor declared for either class, so the compiler won’t complain if you make a copy of a valid descriptor (in fact, it will go as far as to help you, by invoking an implicitly generated copy constructor).
_LIT(KExample, "Fred");
TPtrC original(KExample); // a valid TDesC-derived descriptor
// Shallow copy the type and length into base class object
TDesC copy(original); // Uses the implicit copy constructor
Your code probably won’t crash if it has been written safely, but you will rarely have a valid reason for doing this. The code will fail to work anyway, because TDes and TDesC contain no string data, so in effect are abstract classes.
_LIT(KExample, "Fred");
TPtrC original(KExample); // a valid TDesC-derived descriptor
// Shallow copy the type and length into base class object
TDesC copy(original); // Uses the implicit copy constructor
Your code probably won’t crash if it has been written safely, but you will rarely have a valid reason for doing this. The code will fail to work anyway, because TDes and TDesC contain no string data, so in effect are abstract classes.