Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C++/CLI

Creating Singleton Objects using Visual C++

Rate me:
Please Sign up or sign in to vote.
4.44/5 (23 votes)
8 Jan 2001CPOL 219.4K   279   73  
This article discusses a Creational Pattern called Singleton and explains different approaches for implementing Singleton pattern using a Visual C++ example.
// SingletonView.h : interface of the CSingletonView class
//
/////////////////////////////////////////////////////////////////////////////

// This file is part of Singleton application to demonstrate the concept of Singleton classes.
// This file is generated by AppWizard and is provided "as is" with no expressed or 
// implied warranty.

#include "MsgHndlr.h"

class CSingletonView : public CView
{
protected: // create from serialization only
	CSingletonView();
	DECLARE_DYNCREATE(CSingletonView)

// Attributes
public:
	CSingletonDoc * GetDocument();
	CMsgHndlr	  * m_pMsgHndlr;

// Attributes
protected :
	CString			m_csMessage;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSingletonView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual void OnInitialUpdate();
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CSingletonView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CSingletonView)
	afx_msg void OnMsghndlrMessage1();
	afx_msg void OnMsghndlrMessage2();
	afx_msg void OnMsghndlrMessage3();
	afx_msg void OnMsghndlrCount();
	afx_msg void OnMsghndlrType();
	//}}AFX_MSG
	afx_msg LRESULT OnHandleMessage( WPARAM wParam, LPARAM lParam );
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in SingletonView.cpp
inline CSingletonDoc* CSingletonView::GetDocument()
   { return (CSingletonDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
Kulathu Sarma is working as a Technology Manager for GoldAvenue, a company based in Geneva, Switzerland and responsible for supporting e-business initiatives of GoldAvenue in B2B and B2C Sectors. He has been programming in C/C++ for 9 years and actively working on Patterns for the past 5 years.

Comments and Discussions