Paul Wheatley

The Visual Studio 2005 C++ compiler gives errors if I try to make a managed class global or staic.

Is there a way around this so that I can make a singleton design pattern for the glass where I have one instance that is globally accessible to all other classes

Thanks.

Paul



Re: Visual C++ Language Global access to managed class

Bite Qiu - MSFT

Paul Wheatley wrote:

The Visual Studio 2005 C++ compiler gives errors if I try to make a managed class global or staic.

Is there a way around this so that I can make a singleton design pattern for the glass where I have one instance that is globally accessible to all other classes

Thanks.

Paul

Hi Paul

To implement singleton patterns, you need neither global nor static of managed class, do it like this:

Code Snippet

using namespace System;

public ref class SingletonA{
private:
static SingletonA ^m_hInstance;
public:
static SingletonA^ GetInstance(){
if( m_hInstance == nullptr )
return gcnew SingletonA();
return m_hInstance;
}
void Display(){
Console::WriteLine("fadfa");
}
};

int main(){
SingletonA::GetInstance()->Display();
return 0;
}

hope it helps

rico






Re: Visual C++ Language Global access to managed class

Nishant Sivakumar

In Bite Qiu's example, he forgot to make the ctor private - you'd need to do that too.




Re: Visual C++ Language Global access to managed class

Paul Wheatley

Thanks for the response.

I also need to set m_hInstance to the value returned by gcnew SingletonA();

Paul