T, C, R, M classes
14 janv. 2003 - 19:09

Symbian use the following naming convention to indicate the basic property of a class:

T Simple class (like a typedef). T classes have no constructor and no destructor. They can be allocated on the stack
C C classes have a constructor and a destructor and all derivate from CBase. Instances are always allocated on the heap
R R Classes are proxies for object owned elsewhere (typically a server running in another thread). R classes objects generally have an initialization and a termination function
M A Mixin class. This is pure interface class. In other words an abstract class with only pure virtual methods (no implementation) [1]

Some older version of Symbian OS also use the S prefix for structure definition. This prefix is now obsolete and shall be replaced by T.

The E prefix is also used for enum values (ETrue, EFalse) and K for constants (KErrNone).


T-Classes

T classes are generally used for integers, Booleans and simple other structure types:

TInt   i;        // a counter
TBool  res=ETrue;// a boolean value
TPoint origin;   // a point object having two coordinates TInt X and TInt Y



C-Classes

C classes derivate from Cbase. A C object is allocated on the heap through the use of the new operator. This operator is overloaded by Symbian new(Eleave) to allow a memory protection mechanism called two phase construction :

Class CMyClass : public CBase
{
  ...
}

CMyClass *myObject = new(ELeave) CMyClass ;
...
delete myObject;

R-Classes

R Classes are proxies for object owned elsewhere (typically a server running in another thread). R classes objects have an initialization (generally called Create, Open, ...) and a termination function (Delete, Close,...) :

RTimer myTimer;
myTimer.CreateLocal();
...
myTimer.Delete();

[1] Thanks to Tero for the definition ;-)

Tutorial posted janvier 14th, 2003 by eric

Soumis par Anonymous le jeu, 2003-07-17 16:58.

A printer friendly version would be very nice :)

Soumis par webmaster (non vérifié) le ven, 2003-07-18 00:12.

One day maybe! We've got tons of things to do....

Soumis par Anonymous le mer, 2003-07-23 11:37.

okie. Thanks for this very good & free service so far!

Btw, it's funny that you have choosen NewLC as your name, one of the worst design decision from Symbian IMHO... beside string hanlding... grin. Maybe I should open a TDesC website... LOL.


Soumis par Anonymous le mer, 2003-09-03 14:17.

Mmh... just I was thinking of: www.ConstructL.com --- site under construction...... please Leave

:-) paolo


Soumis par eric le mer, 2003-09-03 17:05.

:-) :-) :-)

Soumis par xtravaga (non vérifié) le dim, 2003-09-14 05:15.

that was funny pablo

Soumis par Anonymous le mar, 2006-09-19 07:17.

Why HBufC is started with H?

Soumis par Anonymous le jeu, 2007-01-11 06:44.

I have read many books but it was not so clear. This is an excellent article. Thanks a ton for Eric and totk.

Soumis par Inder (non vérifié) le jeu, 2007-01-18 03:41.

Thats So simple, because HBufC is used to allocate memory on Heap, It starts with H...

Soumis par kishore (non vérifié) le lun, 2007-03-19 06:14.

hi , AS per article mentioned that a C object is created on Heap is confusing me ,

if CMyObj *obj = new(Eleave) CMyObj;

In the above statement the obj pointer is placed on stack and memory allcated by the CMyObj is on heap.

So we can now conclude that C Class object is located on heap only if iam wrong correct me


Soumis par tOtE (non vérifié) le ven, 2004-09-17 15:10.

A few important things are missing from here.

A T class is not only a typedef. Generally, you should use a T class whenever you have a 'flat' class. Flat means that your class does not own and maintain any externally allocated objects - i.e. it does not have pointer data members. Even if it has, then it must not be responsible for deleting it.

It's like:

class TFlatClass
   {
   public: // constructor
       TFlatClass( TInt aSomething );
   
   public: // data members
       TInt iSomething;
       TPtrC iString;
   };

From the example above you can see that a T class may have a constructor (or more). Actually it may have a destructor too, but is it worth when no externally allocated objects are maintained? It's important to know that T classes are usually small in size. And that's the reason why you're encouraged to use them from stack.

Then C classes: you forgot to mention two important things. First, the constructor of CBase takes care of initializing your data members to zero. That is, you don't have to assign 0 or NULL to your data members inside your CBase-derived object, because it is automatically done. Second, CBase has a virtual destructor, which is very important, because this way if a client has a pointer pointing to your object as CBase* and deletes that object, then the right destructor will be called. Example:

class CYourCBaseObject : public CBase
   {
   // ...
   }

void AnyObject::AnyFunctionL()
   {
   CBase* obj = new (ELeave) CYourCBaseObject;
   delete obj;
   }

You can see that we point to our newly allocated object via a CBase pointer, but despite this the right destructor will be called, when issuing 'delete obj;'.

Any Symbian OS programmers are encouraged to allocate memory on heap when the size of the data exceeds a certain amount of memory. For example, if you know that the size of an arbitrary string may reach (but not exceed) 500 bytes, then do not burden the stack with e.g. an TBuf<500> variable, but use HBufC instead.

Finally note that R classes are usually used in client-server communications. Typical examples are RFile, RFs, RSocket. It's not always true, anyway, because for example RArray has nothing to do with C-S.

Cheers,

tOtE


Soumis par Inder (non vérifié) le mer, 2007-01-17 11:54.

This Bit of Information is pretty good for understanding the Basics of C classes, Thanks

Soumis par soulark le mer, 2007-05-09 08:18.

Thank u for ur amended version. Now I've got started here. And I am sure all of guys here will be great help to me. Smiling
Good luck to everyone!


Soumis par kk (non vérifié) le lun, 2006-03-06 11:22.

This article is really good.... Not confusing, simple and understandable to a Newbie like me too... others can include their views like toTE did, since people like me learn a lot from the replys also...

great job... keep it up...


Soumis par Anonymous le jeu, 2007-01-11 06:47.

Simply excellent. Great job Eric and totE...


copyright 2003-2009 NewLC SARL