从单例类调用继承类的析构函数

Calling destructor of Inherited class from singleton class C++

本文关键字:析构函数 继承 单例类 调用      更新时间:2023-10-16

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

#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;
}

有更好的方法来实现同样的事情吗?

单元素头:

#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;

Your Class (singleton):

#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 YourClassn";}
    virtual ~YourClass(){std::cout << "Hello form Destructorn";}
 private:
     ~YourClass() {}
};

最后你可以这样调用它:

#include "YourClass.hpp"
int main() {
    YourClass::getInstance().Hello();
    return 0;
}

注意使用-pthread标志编译

输出:

Hello form YourClass

Hello form Destructor