I'm having a hard time figuring out how this destructor call
So I have class A with virtual destructor and inline virtual clone function
class A{
public:
A(){ }
virtual ~A(){
qDebug() << "Class A destructor";
}
inline virtual A *clone(){
return NULL;
}
};
Then I have sub-class B also have destructor and clone function return pointer to class A
class B : public A{
public:
B() { }
~B(){
qDebug() << "Class B destructor";
}
A *clone(){
B *temp = new B();
return static_cast<A *>(temp);
}
};
And I have main function and test function and i start do some crazy thing about pointer
A *test(A *input){
return input->clone();
}
int main(){
B temp2;
A *temp = test(&temp2);
delete temp;
return 0;
}
Finally if I have the output is
Class B destructor
Class A destructor
Class B destructor
Class A destructor
and if I'm not using virtual before destructor class A, I have ouput is
Class A destructor
Class B destructor
Class A destructor
So can anyone explain me why virtual make so much different