Click here to Skip to main content
15,906,816 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: how to display a document when clicked on sub menu for a dialog based application? Pin
VC++Maniac10-Dec-08 0:26
VC++Maniac10-Dec-08 0:26 
GeneralRe: how to display a document when clicked on sub menu for a dialog based application? Pin
puppya10-Dec-08 0:27
puppya10-Dec-08 0:27 
AnswerRe: how to display a document when clicked on sub menu for a dialog based application? Pin
VC++Maniac10-Dec-08 0:36
VC++Maniac10-Dec-08 0:36 
GeneralRe: how to display a document when clicked on sub menu for a dialog based application? Pin
puppya10-Dec-08 0:49
puppya10-Dec-08 0:49 
GeneralRe: how to display a document when clicked on sub menu for a dialog based application? Pin
VC++Maniac10-Dec-08 1:14
VC++Maniac10-Dec-08 1:14 
QuestionSingleton class Pin
Nikesh Jagtap9-Dec-08 18:32
Nikesh Jagtap9-Dec-08 18:32 
AnswerRe: Singleton class Pin
_AnsHUMAN_ 9-Dec-08 19:08
_AnsHUMAN_ 9-Dec-08 19:08 
AnswerRe: Singleton class Pin
Jessn9-Dec-08 21:00
Jessn9-Dec-08 21:00 
Singleton!

Intent
Ensure a class only has one instance, and provide a global point of access to it.

Motivation
It's important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.

How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.

A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.

class CSingleton
{
public:
  static CSingleton * getInstance() {
    if (!_singleton) _singleton = new CSingleton;
    return _singleton;
  }

protected:
  CSingleton() { }
  ~CSingleton() { }

private:
  static CSingleton *  _singleton;
};

CSingleton* CSingleton ::_singleton = NULL;


Try to look at singleton destroyers too especially when you singleton holds a database connection or similar resources.

You can also create a SINGLETON TEMPLATE like the one below. In this way your singleton holds a pointer and it ensures that only one instance exists of the object the pointer is pointing at.

template < typename T >
class CSingleton
{
public:
  static CSingleton * getInstance() {
    if (!_singleton) _singleton = new CSingleton;
    return _singleton;
  }

  // Access to the pointer, but NOT the singleton itself
  const T* operator->() { return m_ptr; };  

protected:
  CSingleton() { m_ptr = new T; }
  ~CSingleton() { if (m_ptr) delete m_ptr; }

private:
  static CSingleton *  _singleton;
  T * m_ptr;
};


Hth

--
Jess Nielsen, b.sc.
Security Analyst

http://jessn.blogspot.com/

QuestionHot key problem Pin
MsmVc9-Dec-08 17:49
MsmVc9-Dec-08 17:49 
AnswerRe: Hot key problem Pin
MsmVc9-Dec-08 20:36
MsmVc9-Dec-08 20:36 
GeneralRe: Hot key problem Pin
Code-o-mat10-Dec-08 3:18
Code-o-mat10-Dec-08 3:18 
Questionbmp image on dialog box Pin
VC++Maniac9-Dec-08 17:21
VC++Maniac9-Dec-08 17:21 
AnswerRe: bmp image on dialog box Pin
Mark Salsbery9-Dec-08 17:32
Mark Salsbery9-Dec-08 17:32 
GeneralRe: bmp image on dialog box Pin
VC++Maniac9-Dec-08 17:52
VC++Maniac9-Dec-08 17:52 
GeneralRe: bmp image on dialog box Pin
VC++Maniac9-Dec-08 18:15
VC++Maniac9-Dec-08 18:15 
GeneralRe: bmp image on dialog box Pin
Code-o-mat10-Dec-08 3:25
Code-o-mat10-Dec-08 3:25 
GeneralRe: bmp image on dialog box Pin
VC++Maniac10-Dec-08 3:45
VC++Maniac10-Dec-08 3:45 
GeneralRe: bmp image on dialog box Pin
Code-o-mat10-Dec-08 3:58
Code-o-mat10-Dec-08 3:58 
GeneralRe: bmp image on dialog box Pin
VC++Maniac10-Dec-08 4:08
VC++Maniac10-Dec-08 4:08 
GeneralRe: bmp image on dialog box Pin
Code-o-mat10-Dec-08 4:14
Code-o-mat10-Dec-08 4:14 
AnswerRe: bmp image on dialog box Pin
VC++Maniac10-Dec-08 17:30
VC++Maniac10-Dec-08 17:30 
GeneralRe: bmp image on dialog box Pin
VC++Maniac10-Dec-08 19:28
VC++Maniac10-Dec-08 19:28 
Questionhow to divide panes of status bar of dialog box into equal parts? Pin
puppya9-Dec-08 17:01
puppya9-Dec-08 17:01 
AnswerRe: how to divide panes of status bar of dialog box into equal parts? Pin
_AnsHUMAN_ 9-Dec-08 17:25
_AnsHUMAN_ 9-Dec-08 17:25 
QuestionWhat does this error mean? Pin
monsieur_jj9-Dec-08 15:36
monsieur_jj9-Dec-08 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.