环境:win7 32位,vs2010
源码:
#include <assert.h>
class RefCountedBase {
public:
void ref()
{
++m_refCount;
}
bool hasOneRef() const
{
return m_refCount == 1;
}
unsigned refCount() const
{
return m_refCount;
}
void relaxAdoptionRequirement()
{
}
protected:
RefCountedBase()
: m_refCount(1)
{
}
~RefCountedBase()
{
}
// Returns whether the pointer should be freed or not.
bool derefBase()
{
assert(m_refCount);
unsigned tempRefCount = m_refCount - 1;
if (!tempRefCount) {
return true;
}
m_refCount = tempRefCount;
return false;
}
private:
unsigned m_refCount;
};
template<typename T> class RefCounted : public RefCountedBase
{
private:
RefCounted(const RefCounted&) = delete;
//RefCounted& operator=(const RefCounted&) = delete;
public:
void deref()
{
if (derefBase())
delete static_cast<T*>(this);
}
protected:
RefCounted() { }
~RefCounted()
{
}
};
int main()
{
return 0;
}
报错: 1>d:\hbj\test\test0505\test0505\t0506_2.cpp(55): error C2253: 'RefCounted<T>' : pure specifier or abstract override specifier only allowed on virtual function
1> d:\hbj\test\test0505\test0505\t0506_2.cpp(70) : see reference to class template instantiation 'RefCounted<T>' being compiled
1>
1>Build FAILED.
原因:c++0x新特色:使用或禁用对象的默认函数
vs2010不支持= delete,vs2013是支持的
解决:去掉= delete
参考:http://zh.wikipedia.org/wiki/C%2B%2B11