03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.4 C and T Classes<br />

We've already seen that the naming convention for classes has been chosen to indicate<br />

their main cleanup properties. So far, I've described the cleanup-related properties of C<br />

classes. I'll briefly review them, and then reintroduce T classes, which are quite similar.<br />

C classes are derived from CBase and allocated on the heap. They must, therefore, be<br />

cleaned up when they are no longer needed. Most C classes have a destructor.<br />

C classes are referred to by a pointer – a member variable of some class that owns it, a<br />

member variable of a class that uses it, or an automatic variable.<br />

If a C class is referred to only by a single automatic, in a function that might leave, then the<br />

pointer should be pushed to the cleanup stack.<br />

CBase offers just two things to any C class:<br />

� Zero initialization, so that all member pointers and handles are initially zero, which is<br />

cleanup safe.<br />

� A virtual destructor, so that CBase-derived objects can be properly destroyed from the<br />

cleanup stack.<br />

By contrast, T types are defined as classes or built-in types that don't need a destructor.<br />

They don't need one because they own no data. Examples of T types are as follows:<br />

� Any built-in type: these are given typedefs such as TInt for an unsigned integer.<br />

� Any enumerated type, such as TAmPm, which indicates whether a formatted time of day<br />

is am or pm. All enumerations are Ts, though enumerated constants such as EAm or<br />

EPm begin with E.<br />

� Class types that do not need a destructor, such as TBuf (a buffer for a maximum<br />

of 40 characters) or TPtrC (a pointer to a string of any number of characters). TPtrC<br />

contains a pointer, but it only uses (rather than has) the characters it points to, and so it<br />

does not need a destructor.<br />

Important T classes do not own any data, so they don't need a destructor.<br />

However, they may have pointers, provided that these are uses-a<br />

pointers rather than has-a pointers, like TPtrC's string data<br />

pointer.<br />

T types are normally allocated as automatics, or as member variables of any other kind of<br />

class.<br />

It's possible (but rare) to allocate T class objects explicitly on the heap. If so, you need to<br />

ensure that the heap cell is freed. You can push a T to the cleanup stack using code like<br />

this:<br />

TDes* name = new(ELeave) TBuf; // TDes is a base of TBuf<br />

CleanupStack::PushL(name);<br />

DoSomethingL();<br />

CleanupStack::PopAndDestroy(name)<br />

This invokes CleanupStack::PushL(TAny*). When something pushed as a TAny* is<br />

popped and destroyed, its memory is deallocated, but no destructor is called.<br />

Important<br />

If you forget to derive a C class from CBase, it will be pushed as a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!