locked
How to create a Singleton in C++/CX?

    Question

  • How can I create a singleton class in C++/CX?

    WP7 & IPhone developer 我更喜欢WP7

    Tuesday, May 22, 2012 1:51 AM

Answers

  •  

    Hello,

     

    It depends on what you want to achieve. You can use the native C++ class in C++/CX

     

    class CSingleton  
    {  
        public:  
        static CSingleton * GetInstance()  
        {  
            if(NULL == m_pInstance)  
                m_pInstance = new CSingleton();  
            return m_pInstance;  
        }  
        static void Release()   
        {  
            if(NULL != m_pInstance)  
            {  
                delete m_pInstance;  
                m_pInstance = NULL;  
            }  
        }  
          
        protected:  
            CSingleton()  
            {  
            };  
            static CSingleton * m_pInstance;  
    };  

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Friday, June 1, 2012 8:19 AM
    Wednesday, May 23, 2012 9:18 AM

All replies

  •  

    Hello,

     

    It depends on what you want to achieve. You can use the native C++ class in C++/CX

     

    class CSingleton  
    {  
        public:  
        static CSingleton * GetInstance()  
        {  
            if(NULL == m_pInstance)  
                m_pInstance = new CSingleton();  
            return m_pInstance;  
        }  
        static void Release()   
        {  
            if(NULL != m_pInstance)  
            {  
                delete m_pInstance;  
                m_pInstance = NULL;  
            }  
        }  
          
        protected:  
            CSingleton()  
            {  
            };  
            static CSingleton * m_pInstance;  
    };  

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Friday, June 1, 2012 8:19 AM
    Wednesday, May 23, 2012 9:18 AM
  • Are you trying to ensure that a WinRT component only exists once (single instance) across multiple apps that access it? If so (as far as I know), that would certainly not be possible.

    Perhaps what you need is a WCF service that exposes a single-instance object via a service API. And then have multiple apps on your machine access this service (which will be on the network/internet but not on localhost).


    http://blog.voidnish.com


    Thursday, May 24, 2012 10:38 PM