程序崩潰,因爲堆棧溢出。這是因爲你叫foo()
遞歸永遠
struct derived : public base {
void foo() { base:foo(); }
};
最重要的部分是base:foo();
,查看丟失的結腸?這是同一個標籤,然後調用foo();
base:
foo();
無關,當你建立這個用gcc和選項-Wall
,你會得到兩個警告
a.cpp: In member function 'virtual void derived::foo()':
a.cpp:6:18: warning: label 'base' defined but not used [-Wunused-label]
void foo() { base:foo(); }
^
a.cpp: In function 'int main()':
a.cpp:13:12: warning: deleting object of polymorphic class type 'base' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
delete b;
^
我的編譯器警告我:' '基地':未引用label'。這鈴響了。 –
這就是爲什麼您啓用警告並修復它們的原因。用* gcc *和* clang *試試'-Wall -Wextra'來獲得一組非常合理的基本警告,並添加'-Werror'來強制自己修復它們。我似乎記得'/ W:4'對於MSVC是可以的,但是谷歌或者試用一下。 – hyde