Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private destructor for singleton class

Tags:

c++

singleton

Is it compulsory to have a private destructor for a singleton class.

like image 295
KhanS Avatar asked Apr 01 '10 10:04

KhanS


People also ask

Should destructor be private in Singleton?

Similarly, you should assure the lifetime longevity of the Singleton instance by encapsulating its destructor in a private context.

Can we have a private destructor?

Destructors with the access modifier as private are known as Private Destructors. Whenever we want to prevent the destruction of an object, we can make the destructor private.

How do you destroy a singleton object?

There is no way to destroy a Singleton without breaking the Singleton property. As others have said, maybe a Singleton isn't appropriate here. If the number of instances of the class can go down to zero, then it's not a Singleton.

What is the use of private destructor?

Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private.


3 Answers

If the singleton is implemented as a variable at global scope, it must have a public destructor. Only public members are accessible at global scope.

If it's declared as a static member or static local within its own class, then the destructor may be private. The destructor is called from within class scope, where it is accessible, when the program exits. That is one way to enforce the object being a singleton. Do you need to strongly enforce that? If so, yes. It depends what you mean by "compulsory."

class A{
private:
    ~A() {}
public:
    static A &getGlobalA() {
        static A a2; // <- or here - better technique
        return a2;   // this is initialized upon 1st access
    };               // and destroyed on program exit

    static A a; // <- constructor, destructor accessed from here
};

A A::a; // <- but "called" from here in terms of control flow
like image 182
Potatoswatter Avatar answered Nov 02 '22 23:11

Potatoswatter


This might not be what you are looking for.. But for reference, I use it as follows:

// .h
class Foo {
public:
    static Foo* getInstance();
    static void destroy();
private:
    Foo();
    ~Foo();

    static Foo* myInstance;
};

// .cpp
Foo* Foo::myInstance = NULL;

Foo* Foo::getInstance(){
    if (!myInstance){
        myInstance = new Foo();
    }
    return myInstance;
}
void Foo::destroy(){
    delete myInstance;
    myInstance = NULL;
}

Then at the end of my program, I call destroy on the object. As Péter points out the system will reclaim the memory when your program ends, so there is no real reason. The reason I use a destroy is when Ogre complained that I hadn't released all the memory I allocated. After that I just use it as "good manner", since I like cleaning up after myself.

like image 41
default Avatar answered Nov 02 '22 23:11

default


In my opinion, the destructor of a signleton should be private. Otherwise somewone is able to call 'delete' for your singleton instance. I know, normally nobody will do it. But if we talk about excellence design, it must be resistant to all possible intended or unitnended damages.

With the modern C++ it is allowed to declare even private destructors for statically constructed objects. Here is my code snippet for Singleton:

class Singleton
{
public:
    static Singleton& GetInstance();

    // Before C++ 11 
private:
    Singleton()  {}
    ~Singleton() {}

    Singleton(const Singleton&);            // Without implementation
    Singleton& operator=(const Singleton&); // Without implementation

    // Since C++ 11
private:
    Singleton()  = default;
    ~Singleton() = default;

public:
    Singleton(const Singleton&)            = delete;
    Singleton& operator=(const Singleton&) = delete;
};

Singleton& Singleton::GetInstance()
{
    static Singleton instance;
    return instance;
}
like image 7
Vadim Sukhorukov Avatar answered Nov 03 '22 00:11

Vadim Sukhorukov