C++ protected继承带来的一个与抽象基类指针有关的feature
在写一个关于图形的系统中,有类似以下的代码
class Base {
int y;
protected:
Base() = default;
};
class A: public Base {
int x;
public:
A():x(100) {}
};
class B: protected Base {};
class Logic{
public:
std::unique_ptr<Base> ctx;
Logic():ctx(nullptr){} //weak warning: delete called on 'Base' that is abstract but has non-virtual destructor
};
看到这个warning之后我给基类加上了virtual ~Base();
同时检查了一下另外的两个抽象基类,但另外两个基类我都是protected继承,因此不用考虑被转成基类指针的情况。而上述的warning如果不处理可能会发生内存泄漏