这个问题也可以被命名为“如何在没有 ATL 的情况下进行引用计数”。这里和这里已经提出了一些类似的问题,但在前者中回答了一个不同的问题,并且在这两种情况下都涉及 ATL。我的问题对 C++ 更普遍,而不是关于 COM。
假设我们有一个IUnknown
“接口”,如下所示:
class IUnknown
{
public:
virtual ULONG AddRef() = 0;
virtual ULONG Release() = 0;
virtual ULONG QueryInterface(void * iid, void **ppv) = 0;
};
...让我们添加一些其他接口,它们是虚构 SDK 的一部分:
class IAnimal : public IUnknown
{
public:
virtual IAnimal** GetParents() = 0;
};
class IMammal : public IAnimal
{
public:
virtual ULONG Reproduce() = 0;
};
由于我将要实现几种动物和哺乳动物,我不想在每个类中复制粘贴AddRef()
andRelease()
实现,所以我写道UnknownBase
:
class UnknownBase : public IUnknown
{
public:
UnknownBase()
{
_referenceCount = 0;
}
ULONG AddRef()
{
return ++_referenceCount;
}
ULONG Release()
{
ULONG result = --_referenceCount;
if (result == 0)
{
delete this;
}
return result;
}
private:
ULONG _referenceCount;
};
...这样我就可以用它来实现一个Cat
:
class Cat : public IMammal, UnknownBase
{
public:
ULONG QueryInterface(void *, void**);
IAnimal** GetParents();
ULONG Reproduce();
};
ULONG Cat::QueryInterface(void * iid, void **ppv)
{
// TODO: implement
return E_NOTIMPL;
}
IAnimal** Cat::GetParents()
{
// TODO: implement
return NULL;
}
ULONG Cat::Reproduce()
{
// TODO: implement
return 0;
}
...但是,编译器不同意:
c:\path\to\farm.cpp(42): error C2259: 'Cat' : cannot instantiate abstract class
due to following members:
'ULONG IUnknown::AddRef(void)' : is abstract
c:\path\to\iunknown.h(8) : see declaration of 'IUnknown::AddRef'
'ULONG IUnknown::Release(void)' : is abstract
c:\path\to\iunknown.h(9) : see declaration of 'IUnknown::Release'
我错过了什么?