64,637
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
#endif
class U_Ptr {
public:
U_Ptr(char *s = NULL):str(s) { count = new int(1);}
~U_Ptr()
{
if(--*count == 0) {
delete count;
delete str;
}
}
U_Ptr(const U_Ptr &orig):str(orig.str) {++(*orig.count); }
U_Ptr& operator=(const U_Ptr &rhs)
{
++*rhs.count;
if(--*count == 0) {
delete count;
delete str;
}
str = rhs.str;
count = rhs.count;
return *this;
}
void setstr(char *ip)
{
str = ip;
}
private:
int *count;
char *str;
};
int main()
{
{
U_Ptr a;
{
U_Ptr b(new char('b'));
a.setstr(new char('a'));
a = b;
}
}
_CrtDumpMemoryLeaks();
return 0 ;
}