vendredi 5 février 2016

C++ Singleton inside a namespace, how can I call methods inside?

Perhaps it's pointless to put a Singleton class inside a namespace, but today a colleague asked if this is syntactically correct or not, and I don't know.

// Singleton.h
namespace MySpace
{
    class Singleton
    {       
    public:
        static Singleton& GetInstance()
        {
            static Singleton instance;    
            return instance;
        }

        void ShowMessage(); 
    };
}

// Singleton.cpp
#include "Singleton.h"

namespace MySpace
{
   void Singleton::ShowMessage()
   {
       std::cout << "I'm being called!";
   }
}

How I'm trying to call the functions "ShowMessage()" but on VS2010 shows this error: error C2143: syntax error : missing ';' before '.'

// Incorrect syntax
MySpace::Singleton.GetInstance().ShowMessage();

What is the correct syntax to call ShowMessage()?

Aucun commentaire:

Enregistrer un commentaire