我明白爲什麼發生這種情況,但我堅持試圖解決它「純稱爲虛擬方法」 ......這裏是產生錯誤時,我的代碼做什麼(因此,導致崩潰)我的程序退出時...如何解決
pure virtual method called
SomeClass::~SomeClass()
{
BaseClassObject->SomePureVirtualMethod(this);
}
void DerivedClass::SomePureVirtualMethod(SomeClass* obj)
{
//Do stuff to remove obj from a collection
}
我從來沒有new SomeClass
來電,但我有一個QList<SomeClass*>
我追加SomeClass*
對象。該析構函數的SomeClass
的目的是告訴DerivedClass
從它的QList<SomeClass*>
收集刪除的SomeClass
一個具體實例。
因此,在一個具體的例子...
BaseClass
= Shape
DerivedClass
= Triangle
SomeClass
= ShapeProperties
擁有以Shape
參考所以,我永遠不會有致電new ShapeProperties
但我有一個QList<ShapeProperties*>
內Triangle
。 ShapeProperties
中的析構函數是告訴Triangle
從它的QList<ShapeProperties*>
集合中刪除ShapeProperties
的特定屬性。
那麼我如何知道在引用的構造函數中調用了哪個'this'實例? – user869525
@ user869525:你可以嘗試改說嗎?我不明白... –
我可能誤解了這個,「如果'SomePureVirtualMethod'需要在析構函數中調用,那麼你必須在類的析構函數中調用它,在那裏實際定義你想要的方法。 「 – user869525