{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}
就是因為動態調用是從下向上調用每一個,所以必須的實現. 原文刪除,看下面周星星的評論吧,很不錯的啊!很感謝周星星!
posted on 2006-07-31 20:42 夢在天涯 閱讀(2755) 評論(6) 編輯 收藏 引用 所屬分類: CPlusPlus
代碼認為弄復雜了吧,其實寫成如下就可以了 struct A { virtual ~A() = 0; }; struct B : A { virual ~B() {} }; int main( void ) { B x; } 編譯的時候肯定報A::~A未實現,這是因為普通virtual只調用動態類型的那個函數實現,所以基類的可以不實現;而virtual析構函數則不同,它需要由下往上層層調用,所以每一層都需要實現。 另外,有沒有實現代碼 跟 是否為純虛 是沒有關系的,只要把 A 改為: struct A { virtual ~A() = 0 {} }; 回復 更多評論
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. 回復 更多評論
hpho說 struct A { virtual ~A() = 0 {} }; 應當寫成 struct A { virtual ~A() = 0; }; A::~A() { } 因為C++規定 =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] 回復 更多評論
看標準C++確實有好處, 偶最近也得到了 C++ 03標準文檔, 哈哈, 向兩位高手學習, 在C++博客,你們的文章看得最多, 表示感謝 ! 回復 更多評論
干嘛不用class來定義捏 回復 更多評論