1

I was going through one of the examples of C++, for cloning of an object.

#ifndef CLIPBOARDSTACK_H
#define CLIPBOARDSTACK_H

#include <QStack>

#include "getEntity.h"

class clipboardStack
{
public:
    static clipboardStack *instance()
    {
        if (!inst)
            inst = new clipboardStack;

        return inst;
    }

    void push(getEntity *entity)
    {
        clips.push(entity);
    }

    getEntity *pasteEntity()
    {
        if (clips.count() == 0)
            return 0;

        return clips.last();
    }

    getEntity *pop()
    {
        if (clips.count() == 0)
            return 0;

        return clips.pop();
    }

    bool isEmpty() const
    {
        return clips.empty();
    }

private:
    QStack<getEntity *> clips;
    static clipboardStack *inst;
};

#endif // CLIPBOARDSTACK_H

where getEntity is:

#ifndef GETENTITY_H
#define GETENTITY_H

#include <QGraphicsItem>


class getEntity : public QObject, public QGraphicsItem
{
public:
   getEntity(QObject *parent = 0) : QObject(parent) {}
   virtual ~getEntity() {}

   virtual getEntity *clone()
   {
       return 0;
   }


};

#endif // GENTITY_H

But I couldn't get what exactly the line means.

What is the meaning of the line:

static clipboardStack *instance()
    {
        if (!inst)
            inst = new clipboardStack;




return inst;

}

Can someone explain me what does above line exactly do, and the two classes in brief?

Sidak
  • 15
  • 6
  • 3
    That's the [singleton anti-pattern](http://stackoverflow.com/questions/137975). It's rarely a good idea, and very difficult to implement safely and correctly in C++, so best to forget you saw it. – Mike Seymour Dec 11 '14 at 14:44

2 Answers2

3
static clipboardStack *instance()
    {
        if (!inst)
            inst = new clipboardStack;




return inst;

}

This is a code for singleton pattern. If there is no instance of class clipboardStack, then it would create it else it would return already created instance.

NOTE:- This implementation of singleton is not thread-safe.

ravi
  • 10,994
  • 1
  • 18
  • 36
  • 1
    Also, this singleton pattern is not thread safe so use with caution if you are running a multi-threaded application. – George Houpis Dec 11 '14 at 14:41
  • Also this is not the best implementation of a singleton pattern as, for example, it's suggested to set the constructor as private ( in your example it isn't at all specified) in order to avoid other instance of the same class type. Keeping it private avoid also inheritance. – Stefano Buora Dec 11 '14 at 14:54
0

static clipboardStack *instance() { if (!inst) inst = new clipboardStack;

return inst;

}

This is singleton pattern, usually they write this pattern for having only one instance of the class at any point of time.

If there is no instance for the clipboardstack created. you just create one. next time when someone calls instance(), it delivers the same instance created before. No new instance gets created again.

I guess you have to initialize the clipboardstack pointer to NULL. Its a good programming practice. If you are in a debug mode this might point to uninitialized memory like 0xCDCDCD for instance, which is not null and everytime you call instance(), you will get 0xCDCDCD and you will end up crashing the program.

RC Brand
  • 429
  • 6
  • 10
  • What is the use of using singleton pattern? – Sidak Dec 11 '14 at 14:51
  • They are written for manager classes generally. For instance you have a databsaeManager class. You don't want to have multiple database managers knowingly or unknowingly. If you write a singleton pattern for that class you will ensure that only one instance exists across the program lifetime, so you don't endup two or managers scratching your head who is doing what . This is one usecase of it. – RC Brand Dec 11 '14 at 14:56