Comment: First of, why do you have code outside any function? Statements only make sense when there are within the body of a function, like main
.
Assuming the statements you posted were supposed to go into main
:
Answer:
delete ptr
will call the destructor of A. The compiler will not 'think' any further than this.
Reason: All methods (including the destructor) are non-virtual by default. In your case, you did not specify that the destructor should be virtual. The compiler sees that you are calling the destructor on a A*
pointer, so it calls the destructor of A
.
What if I had specified that Class A destructor was virtual? Would it still call the destructor of Class A?
Answer: If it were virtual
, it would call the destructor of B
, because the actual type of the object would be determined during the execution of the program.
See more about virtual functions and polymorphism here.