引用自
#include <malloc.h>
#include <assert.h>
//
// void * operator new(size_t size)
// {
// printf("调用全局new\n");
// void *p = malloc(size);
// return (p);
// }
// void * operator new[](size_t size)
// {
// printf("调用全局new[]\n");
// void *p = malloc(size);
// return (p);
// }
// void operator delete(void *p)
// {
// free(p);
// printf("调用全局delete\n");
// }
//
// void operator delete[](void *p)
// {
// free(p);
// printf("调用全局delete[]\n");
//
// }
class stackonly
{
private:
void * operator new(size_t Size)
{
printf("stackonly调用重载new\n");
return malloc(Size);
}
void * operator new[](size_t Size)
{
printf("stackonly调用重载new[]\n");
return malloc(Size);
}
void operator delete(void *p)
{
free(p);
printf("stackonly调用重载delete\n");
}
void operator delete[](void *p)
{
free(p);
printf("stackonly调用重载delete[]\n");
}
public:
stackonly():a(-1),b(-1),c(-1){printf("stackonly调用构造函数\n");}
~stackonly(){printf("stackonly调用析构函数\n");}
void print(){printf("%d %d %d\n",a,b,c);}
public:
int a;
int b;
int c;
};
class heaponly
{
private:
heaponly():a(-2),b(-2),c(-2){printf("heaponly调用构造函数\n");}
~heaponly(){printf("heaponly调用析构函数\n");}
public:
static heaponly* GetInstance(); //单件模式
static void ReleaseInstance();//释放实例引用
void print(){printf("%d %d %d\n",a,b,c);}
private:
int a;
int b;
int c;
static int refCount;
public:
static heaponly *hp;
};
heaponly* heaponly::hp = NULL;
int heaponly::refCount = 0;
heaponly* heaponly::GetInstance()
{
if (hp == NULL)
{
hp =new heaponly;
assert(hp);
}
++refCount;
return hp;
}
void heaponly::ReleaseInstance()
{
if (refCount == 0)
{
return;
}
--refCount;
if (0 == refCount)
{
delete hp;
hp =NULL;
}
}
void main()
{
stackonly stackOnly;
stackOnly.print();
//stackonly *p= new stackonly;//编译报错
//heaponly hep;//编译报错
heaponly *pHeapOnly = heaponly::GetInstance();
pHeapOnly->print();
heaponly *pAnother =heaponly::GetInstance();
pAnother->print();
pHeapOnly->ReleaseInstance();
pAnother->ReleaseInstance();
}
http://blog.csdn.net/wonengxing/article/details/6862971
1 在C++中如何限制一个类对象只在堆上分配?
仿照设计模式中的单实例模式或者工厂模式来解决,这里采用单实例模式方式来说明。
将类的构造函数属性置为private,同时提供static成员函数getInstance,在函数中new一个新对象,然后返回对象指针或者引用。这样实现的类可以保证只可以在堆上分配对象。
2 在C++中如何限制一个类对象只在栈上分配?
重载类的new操作符,使重载后的new操作符的功能为空。这样就使外层程序无法在堆上分配对象,只可以在栈上分配。
测试代码如下:
#include <stdio.h>#include <malloc.h>
#include <assert.h>
//
// void * operator new(size_t size)
// {
// printf("调用全局new\n");
// void *p = malloc(size);
// return (p);
// }
// void * operator new[](size_t size)
// {
// printf("调用全局new[]\n");
// void *p = malloc(size);
// return (p);
// }
// void operator delete(void *p)
// {
// free(p);
// printf("调用全局delete\n");
// }
//
// void operator delete[](void *p)
// {
// free(p);
// printf("调用全局delete[]\n");
//
// }
class stackonly
{
private:
void * operator new(size_t Size)
{
printf("stackonly调用重载new\n");
return malloc(Size);
}
void * operator new[](size_t Size)
{
printf("stackonly调用重载new[]\n");
return malloc(Size);
}
void operator delete(void *p)
{
free(p);
printf("stackonly调用重载delete\n");
}
void operator delete[](void *p)
{
free(p);
printf("stackonly调用重载delete[]\n");
}
public:
stackonly():a(-1),b(-1),c(-1){printf("stackonly调用构造函数\n");}
~stackonly(){printf("stackonly调用析构函数\n");}
void print(){printf("%d %d %d\n",a,b,c);}
public:
int a;
int b;
int c;
};
class heaponly
{
private:
heaponly():a(-2),b(-2),c(-2){printf("heaponly调用构造函数\n");}
~heaponly(){printf("heaponly调用析构函数\n");}
public:
static heaponly* GetInstance(); //单件模式
static void ReleaseInstance();//释放实例引用
void print(){printf("%d %d %d\n",a,b,c);}
private:
int a;
int b;
int c;
static int refCount;
public:
static heaponly *hp;
};
heaponly* heaponly::hp = NULL;
int heaponly::refCount = 0;
heaponly* heaponly::GetInstance()
{
if (hp == NULL)
{
hp =new heaponly;
assert(hp);
}
++refCount;
return hp;
}
void heaponly::ReleaseInstance()
{
if (refCount == 0)
{
return;
}
--refCount;
if (0 == refCount)
{
delete hp;
hp =NULL;
}
}
void main()
{
stackonly stackOnly;
stackOnly.print();
//stackonly *p= new stackonly;//编译报错
//heaponly hep;//编译报错
heaponly *pHeapOnly = heaponly::GetInstance();
pHeapOnly->print();
heaponly *pAnother =heaponly::GetInstance();
pAnother->print();
pHeapOnly->ReleaseInstance();
pAnother->ReleaseInstance();
}