mercredi 29 novembre 2017

Singleton class with smart pointers and destructor being called

I want to create a singleton class such that when all pointers to the class go away the destructor is called.

#include <memory>
#include <iostream>

class MyClass {
public:
    uint8_t str[50]; //some random data
    MyClass() {LOG("constructor Called"); }
    ~MyClass() {LOG("destructor Called");}
    static std::shared_ptr<MyClass> &Get();

private:
     static std::shared_ptr<MyClass> instance;

};

std::shared_ptr<MyClass> MyClass::instance=NULL;


std::shared_ptr<MyClass> &MyClass::Get()
{
    if (instance == NULL)
    {
        instance= std::shared_ptr<MyClass>(new MyClass());
        return instance;
     }
     return instance;
}

int main()
{

    std::shared_ptr<MyClass> &p1 =MyClass::Get();

     printf("We have %" PRIu32, p1.use_count());
     if (1)
     {
        std::shared_ptr<MyClass> &p2 =MyClass::Get();//this should not  
                                                 //  create a new class
        printf("We have %" PRIu32, p1.use_count());  //this should be two...
        printf("We have %" PRIu32, p2.use_count());  //this should be two...
        //when p1 goes out of scope here it should not call destructor
      }
      printf("We have %" PRIu32, p1.use_count());

     //now destructor should be called
      return 0;
}

The above code does not work and I was wondering if anyone had a suggestion?

Aucun commentaire:

Enregistrer un commentaire