如何定义一个只能在堆上生成对象的类
如何定义一个只能在堆上生成对象的类2016-11-10
class AA
{
public:
static AA* GetObject(int a)
{
return new AA(a);
}
~AA()
{
delete this;
}
protected:
AA(int a)
{
_a = a;
}
private:
int _a;
};
void test()
{
AA *a = AA::GetObject(10);
}class AA
{
public:
void destory()
{
delete this;
}
private:
~AA()
{
}
};
void test2()
{
AA *a=new AA; //可以
a->destory();
}