Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to create singleton class in Symbian

From Wiki
Jump to: navigation, search
Article Metadata
Created: savaj (07 Mar 2009)
Last edited: hamishwillee (26 Jul 2012)


What is Singleton class?

A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist.

How to implement it in symbian?

Symbian provided CCoeStatic base class for creating singleton class. To omplement singleton pattern you have to derive your class from CCoeStatic class.

Header file

#include <coemain.h>
class CMySingleton: public CCoeStatic
static CMySingleton* GetInstance();
void InitializeL();
void UpdateData(const TDesC8& aText);
TInt iNumber;

Implementation file.

static const TUid KUidMySingleton = { 0x10000123 };
CMySingleton* CMySingleton::GetInstance()
CMySingleton* pInstance = (CMySingleton*)CCoeEnv::Static(KUidMySingleton);
if (!pInstance)
pInstance = new (ELeave) CMySingleton();
return pInstance;
void CMySingleton::InitializeL()
iNumber = 8; //initialize data member here.
void CMySingleton::UpdateData(const TDesC8& aText)

How to use it?

Add header file mysingleton.h in your class. And use it in following manner.
TBuf8<20> data( _L8("singleton") );
CMySingleton* pMySingleton = CMySingleton::GetInstance(); //if instance of class is available then it return it, else it will create new instance
This page was last modified on 26 July 2012, at 02:10.
24 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.