class Reference {
private:
ushort ref_count;
public:
operator delete() {
ref_count--;
if(ref_count <= 0) {
delete this;
}
}
};
这是我想要达到的基本想法。如何使析构函数仅由运算符删除调用,因此我不必添加&#34; free&#34; (或其他)虚拟方法?
这是定义引用计数类的方式:
class Texture: public Reference {
public:
uint id;
public:
~Texture() {
// Problem: this is called when delete is called,
// not only from the operator delete() override
glDeleteTextures(1, id);
}
};
这适用于游戏引擎,我只需要在未加载资源时加载资源,并且仅在删除对它们的所有引用时才释放资源。我不想要shared_ptr,我想要一个更简单的包装器。