class CastStrategy
{
public:
CastStrategy();
virtual ~CastStrategy();
virtual bool CastSpell(Caster *lpCaster, uint64_t ullTarget) = 0;
};
会报链接错误!
class CastStrategy
{
public:
CastStrategy();
//virtual ~CastStrategy();
virtual bool CastSpell(Caster *lpCaster, uint64_t ullTarget) = 0;
};
会出现:
CastStrategy.h:17: warning: ‘class CastStrategy’ has virtual functions but non-virtual destructor CastStrategy.h:27: warning: ‘class EnemyStrategy’ has virtual functions but non-virtual destructor
这个类有一个纯虚函数,所以它是抽象的,而且它有一个虚析构函数,所以不会产生析构函数问题。但这里还有一件事:必须提供纯虚析构函数的定义:
CastStrategy::~CastStrategy() { } // 纯虚析构函数的定义
这个定义是必需的,因为虚析构函数工作的方式是:最底层的派生类的析构函数最先被调用,然后各个基类的析构函数被调用。这就是说,即使是抽象类,编译器也要产生对~CastStrategy的调用,所以要保证为它提供函数体。如果不这么做,链接器就会检测出来,最后还是得回去把它添上。

浙公网安备 33010602011771号