標籤:

為什麼編譯器不能「合成「純虛析構函數的函數定義??

雖然編譯器採取」分離編譯模型「,但是編譯器完全可以在編譯包含純虛析構函數的class時」合成「出純虛析構函數的函數定義體,不是嗎?


C++標準§ 10.4第2條:

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function. A virtual function is specified pure by using a pure-specifier in the function declaration in the class definition. A pure virtual function need be defined only if called with, or as if with, the qualified-id syntax.

C++標準§ 12.4第4條:

If a class has no user-declared destructor, a destructor is implicitly declared as defaulted. An implicitlydeclared destructor is an inline public member of its class.

如果想讓一個函數成為純虛函數,必須得聲明它。而一旦聲明了析構函數,不管它是不是(純)虛函數,你都必須要定義它。


class Base
{
public:
virtual ~Base() = 0;
}

Base::~Base() = default;


推薦閱讀:

編程語言是語法比較重要還是編譯器的具體實現比較重要?
符號表和抽象語法樹是什麼關係?兩者在編譯器設計中是否必需?
程序設計中,堆和棧比較重要。棧存取速度大於堆,而且編譯器可以修改棧大小,這個值可以隨意設置嗎?
GPU編譯器開發怎麼樣,前景如何?

TAG:C | 編譯器 |