Destructing derived class by deleting base class

后端 未结 3 390
暗喜
暗喜 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:30

    Is this possible in its destructor or do I have to implement a virtual Destroy() method or something like that?

    Make destructor of A virtual.

     virtual ~A() { std::cout << "A destroyed!" << std::endl; }
    

    If your class have virtual methods, it should use virtual destructor. At least some compilers will complain if you aren't using virtual destructor in class with virtual methods.

提交回复
热议问题