{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}
就是因為動態(tài)調(diào)用是從下向上調(diào)用每一個,所以必須的實現(xiàn). 原文刪除,看下面周星星的評論吧,很不錯的啊!很感謝周星星!
posted on 2006-07-31 20:42 夢在天涯 閱讀(2750) 評論(6) 編輯 收藏 引用 所屬分類: CPlusPlus
代碼認(rèn)為弄復(fù)雜了吧,其實寫成如下就可以了 struct A { virtual ~A() = 0; }; struct B : A { virual ~B() {} }; int main( void ) { B x; } 編譯的時候肯定報A::~A未實現(xiàn),這是因為普通virtual只調(diào)用動態(tài)類型的那個函數(shù)實現(xiàn),所以基類的可以不實現(xiàn);而virtual析構(gòu)函數(shù)則不同,它需要由下往上層層調(diào)用,所以每一層都需要實現(xiàn)。 另外,有沒有實現(xiàn)代碼 跟 是否為純虛 是沒有關(guān)系的,只要把 A 改為: struct A { virtual ~A() = 0 {} }; 回復(fù) 更多評論
A destructor can be declared virtual(10.3) or pure virtual(10.4);if any object of that class or any derived class are created in the program, the destructor shall be defined. If a class has a base class with a virtual destructor, its destructor (whether user-or implicitly-declared) is virtual. 回復(fù) 更多評論
hpho說 struct A { virtual ~A() = 0 {} }; 應(yīng)當(dāng)寫成 struct A { virtual ~A() = 0; }; A::~A() { } 因為C++規(guī)定 =0 和 {} 不能同時出現(xiàn)。 回復(fù) 更多評論
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] 回復(fù) 更多評論
看標(biāo)準(zhǔn)C++確實有好處, 偶最近也得到了 C++ 03標(biāo)準(zhǔn)文檔, 哈哈, 向兩位高手學(xué)習(xí), 在C++博客,你們的文章看得最多, 表示感謝 ! 回復(fù) 更多評論
干嘛不用class來定義捏 回復(fù) 更多評論