对于单例而言,只能有一个实例。
对于多态而言,可能有多个实例。
为了在多态中运用单例的思想,可以这样写。
//base.h
class BASE {
public:
static BASE& GetInstance(const int type);
BASE() = default;
virtual ~BASE() = default;
};
//base.cpp
#include "base.h"
#include "derived.h"
BASE& BASE::GetInstance(const int type) {
if (type == "0") {
static Base A;
return A;
} else {
static Derived B;
return Derived;
}