tags:

views:

1083

answers:

4

On Jon's site he has thisvery elegantly designed singleton in C# that looks like this:

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

I was wondering how one would code the equivalent in C++? I have this but I am not sure if it actually has the same functionality as the one from Jon. (BTW this is just a Friday exercise, not needed for anything particular).

class Nested;

class Singleton
{
public:
  Singleton() {;}
  static Singleton& Instance() { return Nested::instance(); }

  class Nested
  { 
  public:
    Nested() {;}
    static Singleton& instance() { static Singleton inst; return inst; }
  };
};

...


Singleton S = Singleton::Instance();
+1  A: 

As far as I am aware, inheritable Singleton behaviour is not possible in C++ or Java, (or at least it wasn't on earlier versions of JDK). This is a C# specific trick. Your subclasses will have to explicitly implement the protocol.

ConcernedOfTunbridgeWells
Am I missing something? I don't think inheritance is entering the picture even in the C# example.
Michael Burr
It would definitely work in Java - that's where I originally got the idea from.
Jon Skeet
Perhaps I missed something, the OP gave the impression he was after a generic behaviour. I remember trying to make an inheritable singleton in an early JDK and finding out that Java could not do it; IIRC trad C++ won't either, although you could probably do it with templates. Now I look at the article again it's not specifically mentioned, so maybe he's just looking at how to implement a Singeton. The GOF book might have an example in C++.
ConcernedOfTunbridgeWells
@ConcernedOfTunbridgeWells: Alexandrescu's "Modern C++ Design" has (as was mentioned elsewhere) treated the subject extensively. Read up on that if you want to know how to do this in C++ generically.
sbi
+19  A: 

This technique was introduced by University of Maryland Computer Science researcher Bill Pugh and has been in use in Java circles for a long time. I think what I see here is a C# variant of Bill's original Java implementation. It does not make sense in a C++ context as the current C++ standard is agnostic on parallelism. The whole idea is based on the language guarantee that the inner class will be loaded only at the instance of first use, in a thread safe manner. This does not apply to C++. (Also see this Wikipedia entry)

Vijay Mathew
thanks for the info!
Anders K.
+5  A: 

You'll find a great discussion of how to implement a singleton, along with thread-safety in C++ in this paper.

http://www.aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf

navigator
+1 for mentioning this very good article on the topic by Meyers and Alexandrescu.
sbi
Alexandrescu also has a whole chapter on implementing singletons in his book "Modern C++ Design". The content is probably similar.
luke