从单例类调用继承类的析构函数
Calling destructor of Inherited class from singleton class C++
我有一个基类,它是单例的,它是抽象的。基本上我是用一个继承的类实例化。我有抽象的构造函数和析构函数作为保护。我想有一个基类,其中一些函数有定义,其中一些是纯虚的,子类在该接口中实现方法,但我希望它是单例的。
#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
相关文章:
- C++ std::vector 中的虚拟析构函数继承
- 继承的类析构函数
- 抽象类析构函数与继承"Shutdown"函数
- C++虚拟继承、虚拟析构函数和 dynamic_cast<void*>
- 如果不手动完成,子类是否继承父类的析构函数?
- 从 std::streambif 继承时不兼容析构函数编译器警告
- 多重继承析构函数调用他自己和父析构函数?c++
- 为什么我在使用组合而不是继承时得到 C4624(无法访问基类析构函数)
- 抽象基类中的析构函数保护不会在 C++ 中继承
- C++继承和构造函数、析构函数
- 从 STL 容器继承并删除"新"运算符以防止由于缺少虚拟析构函数而导致未定义的行为是否有意义?
- 虚拟继承中的析构函数
- 复制、移动、交换、赋值和析构函数的C++继承?我需要哪个
- 在不使用虚拟析构函数的情况下删除继承的对象时中止
- C++析构函数继承
- C++非抽象析构函数继承
- 构造函数和析构函数继承
- C++析构函数-继承上下文中的显式调用
- 非虚拟平凡析构函数 + 继承
- C++构造函数/析构函数继承