views:

75

answers:

2

I've looked around, I need an example for Singleton class that works across 2 or more C++/CLI files.

How do you declare a singleton in C++/CLI, not C# ?

How do you share that singleton across two or more C++/CLI files?

I keep getting Variable redefinitions when I try to share that singleton.

A: 

These may be useful

Josh
These do not work with C++.NET. they work only either on C++ or C#.NET
buttercup
A: 

This is for C++/CLI, not ".NET Managed Extensions for C++" aka C++.NET. Don't use the Managed Extensions (Visual Studio 2002-2003), they're buggy.

ref class Singleton
{
private:
  Singleton() {}
  Singleton(const Singleton%) { throw gcnew System::InvalidOperationException("singleton cannot be copy-constructed"); }
  static Singleton m_instance;

 public:
  static property Singleton^ Instance { Singleton^ get() { return %m_instance; } }
};

As for "across multiple files", other compilation units in the same project use #include, other assemblies use a reference (or #import). Then there won't be any redefinition issues.

Ben Voigt