2012年(158)
分类: C/C++
2012-11-23 15:12:04
问:
class Deleteable
{
public:
virtual ~Deleteable() = 0
{}
virtual void deleteSelf()
{ delete this;
}
};
在g++3.4.2中为什么无法编译通过(vc++8.0可以)?
答:
ISO/IEC 14882:2003(E)第10.4.2中指出:
[Note: a function declaration
cannot provide both a pure-specifier and a definition
—end note]
[Example:
struct C {
virtual void f() = 0 { }; //
ill-formed
};
—end
example]
所以在这一点上,g++是符合C++标准的,标准方法应当写成:
class
Deleteable
{
public:
virtual ~Deleteable() = 0;
virtual void deleteSelf()
{ delete this; }
};
inline
Deleteable::~Deleteable()
{
}