从单例类C ++

时间:2016-09-01 07:10:23

标签: c++ inheritance

我有一个单例的基类,它是抽象的。所以基本上我用其中一个继承的类实例化。我有抽象的构造函数和析构函数作为受保护。我希望有一些基类,其中一些函数有定义,其中一些是纯虚拟和子类在该接口中实现方法,但我希望它是单例。

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

class Base {
    protected:
        Base() {}
        virtual ~Base() { 
            cout<< "base class destructor \n";
        } 
    private:
        static Base *instance;

    public:
        virtual void overrideMe() = 0;
        static Base* getinstance(const char*);
        static void resetInstance(){
            delete instance;
        }
        virtual void testmethod();

};
class Derived1 : public Base{
    friend class Base;
    protected:
        Derived1(){
            cout<<" in Derived1 constructor \n";
        }
        virtual ~Derived1(){
            cout<< "in Derived1 destructor \n";
        }

    public:
        void overrideMe(){
            cout<< "in Derived1 \n";
        }
         void testmethod(){
             cout<< "testmethod of Derived1 \n ";
             overrideMe();
         }
};
class Derived2 : public Base{
    friend class Base;
    protected:
            Derived2(){
            cout<<" in Derived2 constructor \n";
        }
        virtual ~Derived2(){
            cout<< "in Derived2 destructor \n";
        }
    public:

        void overrideMe(){
            cout<< "in Derived2 \n";
        }
        void testmethod(){
            cout<< "testmethod of Derived2 \n ";
            overrideMe();
        }


};
Base* Base::instance = NULL;

void Base::testmethod() {
    cout << "Testing :)\n";
}

Base* Base::getinstance(const char* type) {
    if(instance == NULL){
        if(std::strcmp(type, "Derived1") == 0)
            instance = new Derived1();
        if(std::strcmp(type, "Derived2") == 0)
            instance = new Derived2();
    } 
    return instance;
}


int main() {

    Base *instanceA = Base::getinstance("Derived1"); 
  //  Derived1* ob = new Derived1();
    instanceA->testmethod();
    Base::resetInstance();
    return 0;
}

有没有更好的方法来达到上述相同的目的?

1 个答案:

答案 0 :(得分:0)

Singleton标题:

#include <memory>
#include <mutex>

template <typename T>
class Singleton {
 public:
  Singleton(const Singleton&) = delete;
  const Singleton& operator=(const Singleton&) = delete;

  static T& getInstance() {
    std::call_once(m_flag, [] { m_instance.reset(new T); });
    return *m_instance.get();
  }

 protected:
  Singleton() {}

 private:
  static std::unique_ptr<T> m_instance;
  static std::once_flag m_flag;
};

template <typename T>
std::once_flag Singleton<T>::m_flag;

template <typename T>
std::unique_ptr<T> Singleton<T>::m_instance;

你的班级(单身人士):

#include <iostream>
#include "Singleton.hpp"



class YourInterface {
public:
    virtual void Hello() = 0;
    virtual ~YourInterface() {}
};

class YourClass : public YourInterface, public Singleton<YourClass> {
  friend class Singleton<YourClass>;

 public:
    virtual void Hello () {std::cout << "Hello form YourClass\n";}
    virtual ~YourClass(){std::cout << "Hello form Destructor\n";}

 private:
     ~YourClass() {}

};

最后你可以这样称呼它:

#include "YourClass.hpp"

int main() {
    YourClass::getInstance().Hello();
    return 0;
}

请记住使用-pthread标志进行编译

输出:

Hello form YourClass

Hello形式析构函数