class Test
{
public:Test(){cout << "构造函数\n";}~Test(){cout << "析构函数\n";}
};
void *operator new(size_t nsize)
{void *ptmp = std::malloc(nsize);printf("申请内存:%d,%p\n",nsize, ptmp);return ptmp;
}void operator delete(void *p)
{printf("释放内存:%p\n",p);std::free(p);
}
int main()
{cout << "start\n";{std::weak_ptr<Test> wp;{std::shared_ptr<Test> pInt(new Test);wp = pInt;}cout << "----------1------------\n";if (wp.expired()){cout << "不可用\n";} cout << wp.use_count() << endl;cout << "----------2------------\n";}cout << "end\n";system("pause");return 0;
}
结果:
int main()
{cout << "start\n";{std::weak_ptr<Test> wp;{std::shared_ptr<Test> pInt(new Test);wp = pInt;}cout << "----------1------------\n";if (wp.expired()){cout << "不可用\n";} cout << wp.use_count() << endl;wp.reset();cout << "----------2------------\n";}cout << "end\n";system("pause");return 0;
}