#include <stdio.h>
#include <memory>
#include <string>
using namespace std;
struct Base
{
public:
Base(const std::string & name):m_name(name)
{
printf("Create Base:%s\n", m_name.c_str());
}
~Base()
{
printf("Delete Base:%s\n", m_name.c_str());
}
virtual void print() = 0;
protected:
std::string m_name;
};
class Derive:public Base
{
public:
Derive(const std::string & name):Base(name)
{
printf("Create Derive:%s\n", m_name.c_str());
}
virtual ~Derive()
{
printf("Delete Derive:%s\n", m_name.c_str());
}
void print()
{
printf("This is Derive\n");
}
};
int main()
{
std::shared_ptr<Base> d1(new Derive("d1"));
d1.reset();
printf("\n\n");
Base * d2 = new Derive("d2");
delete d2;
return 0;
}
虚析构函数
最新推荐文章于 2023-10-29 21:05:56 发布