1

Hi I have an snippet of code that is a singleton class ( I believe) that uses 'new' and some other scope and static qualifiers in a way I have not seen before. Because I do not know what it is called I cannot find any information on what it does or its purpose. Code below:

In the myClass.h file I have

class myClass
{
private:
    static myClass sm_myClass;
    myClass();
public:
    static void Create(void);
};

and in the myClass.cpp I have

#include "myClass.h"
#include <new>

myClass* p_myClass = NULL;

myClass myClass::sm_myClass;

myClass::myClass()
{
}

void myClass::Create(void)
{
    p_myClass = &sm_myClass;

    new (p_myClass) myClass();
}

My questions are:

1 - What is happening at line 6 ( myClass myClass::sm_myClass; ) of the .cpp file

2 - What is happening at line 16 ( new (p_myClass) myClass(); ) of the .cpp file

3 - What is the overall purpose of using a class in this way?

I really hope that this question is not a repeat. I searched for an answer for a while but didn't know what terms to even search. Thanks for any help.

SBBG
  • 33
  • 5
  • FWIW if you wan to use a singleton see: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – NathanOliver Aug 05 '16 at 19:32
  • 1
    You might also want to read [What is so bad about singletons?](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons?rq=1) – Bo Persson Aug 05 '16 at 19:34
  • 1
    The object `sm_myClass` gets constructed at program startup. The call to placement new attempts to construct a new object on top of the already existing one, giving undefined behavior. – Pete Becker Aug 05 '16 at 19:40
  • @PeteBecker: It's actually okay to just placement-new over an existing object, though obviously this is not recommended. IIRC, it's only UB if you relied on the destructor for some side effects. – GManNickG Aug 05 '16 at 20:10
  • @GManNickG -- in limited cases it may be okay, but that's advanced technique, completely inappropriate in a beginner's code. – Pete Becker Aug 05 '16 at 21:20

2 Answers2

5

1 - What is happening at line 6 ( myClass myClass::sm_myClass; ) of the .cpp file

A static class member is defined.

2 - What is happening at line 16 ( new (p_myClass) myClass(); ) of the .cpp file

A placement new is specified to reuse the memory allocated for p_myClass

3 - What is the overall purpose of using a class in this way?

There are cases when singletons might come in handy, but in general it's better to pass interfaces around, instead of tight coupling client code to myClass.


Don't use new (placement new or not) to initialize singleton instances. Use the Scott Meyer's singleton implementation instead, which guarantees thread safety:

class myClass
{
private:
    myClass();
public:
    static myClass& instance() {
         static myClass theInstance;
         return theInstance;
    }
};

The instance will be created at first access to the instance() function, and stay the same for any subsequent calls.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
3

1) static member variables need to have there storage allocated in one compilation unit (.cpp/.cxx). This definition establishes that.

2) This is called placement new, it allows you to construct an object into already allocated memory. This usage is flawed since the object has already been constructed and that construction is being overwritten.

3) Sometimes it is handy to have a global service encapsulated into an object and having a singleton provides an easy way for clients of that service to get access to it.

nate
  • 1,631
  • 11
  • 16