問題基於以下類層次結構。C++虛擬析構函數
Base2* d3 = new Der3();
d3->v1();
delete d3;
輸出是:
Base1
Base1
Der1
Base2
Der3
v1 Base2
~Base2
我也得到一個exception.Why(只應該產生記憶leack)
class Base1
{
public:
Base1() {std::cout << "Base1" << std::endl; }
~Base1() {std::cout << "~Base1" << std::endl; }
virtual void v1() { std::cout << "v1 Base1" << std::endl; }
void v2() {std::cout << "v2 Base1" << std::endl;}
void v3() {std::cout << "v3 Base1" << std::endl;}
};
class Base2
{
public:
Base2() {std::cout << "Base2" << std::endl; }
~Base2() {std::cout << "~Base2" << std::endl; }
void v1() { std::cout << "v1 Base2" << std::endl; }
void v2() {std::cout << "v2 Base2" << std::endl;}
void v3() {std::cout << "v3 Base2" << std::endl;}
};
class Der1 : public Base1
{
public:
Der1() {std::cout << "Der1" << std::endl; }
~Der1() {std::cout << "~Der1" << std::endl; }
virtual void v1() { std::cout << "v1 Der1" << std::endl; }
virtual void v2() {std::cout << "v2 Der1" << std::endl;}
void v3() {std::cout << "v3 Der1" << std::endl;}
};
class Der2 : public Base1,public Der1
{
public:
Der2() {std::cout << "Der2" << std::endl; }
~Der2() {std::cout << "~Der2" << std::endl; }
virtual void v1() { std::cout << "v1 Der2" << std::endl; }
void v2() {std::cout << "v2 Der2" << std::endl;}
void v3() {std::cout << "v3 Der2" << std::endl;}
};
class Der3 : public Base1,public Der1,public Base2
{
public:
Der3() {std::cout << "Der3" << std::endl; }
~Der3() {std::cout << "~Der3" << std::endl; }
virtual void v1() { std::cout << "v1 Der3" << std::endl; }
void v2() {std::cout << "v2 Der3" << std::endl;}
void v3() {std::cout << "v3 Der3" << std::endl;}
};
析構函數不是虛擬的... – interjay
你不確定的行爲,因爲你要刪除想到了一個指向類型不匹配最初分配的對象的類型和指針的類型不有一個虛擬析構函數(並且是實際分配類型的基類)。 –
@interjay從標題判斷,我猜他知道這一點。他的問題是,他爲這種情況分配了某種定義的行爲,實際上它並未定義。 –