//1 只能在堆上创建对象
class HeapOnly
{
public:
static HeapOnly* CreateInstance()
{
HeapOnly* obj = new HeapOnly;
obj->m = 2;
return obj;
}
static void DeleteInstance(HeapOnly* obj)
{
if (obj)
delete obj;
}
private:
HeapOnly();
~HeapOnly();
private:
int m;
};
//只在栈上创建
class StackOnly
{
public:
StackOnly()
{
cout << "constructor" << endl;
}
~StackOnly()
{
cout << "destructor" << endl;
}
private:
void* operator new (size_t size);
};
//不能被继承
class MakeFinal
{
private:
MakeFinal();
~MakeFinal();
};
class NotInheritClass :virtual public MakeFinal
{
public:
NotInheritClass();
~NotInheritClass();
};
class Test :public NotInheritClass
{
public:
Test();
~Test();
};
设计一个类不能被继承 2.设计一个类只能在堆上创建对象。 3.设计一个类只能在栈上创建对象
最新推荐文章于 2020-05-12 11:00:12 发布