Destructing derived class by deleting base class

后端 未结 3 436
暗喜
暗喜 2021-01-25 16:39

I have a situation that looks like the following code:

#include 

class A
{
public:
    A() { std::cout << \"A created!\" << std::end         


        
3条回答
  •  长发绾君心
    2021-01-25 17:09

    As a rule of thumb, if any of your methods are virtual, the destructor must also be virtual. If it isn't, the declared type of a variable decides which destructor gets called, which is almost never what you want. 99.9% of all cases, you want the destructor from the runtime type.

提交回复
热议问题