list管理对象

/*
	管理类的内存,要通重载
	类,函数是共享的,数据是每个对象私有的
*/
#include<iostream>
#include<list>
#include<Windows.h>

using namespace std;
class myclass; //类的声明

struct info
{
	myclass *p;	//指针,内存首地址(对象的地址)
	int n;		//代表多少个对象。
};

list<info> myclasslist;	//双链表管理对象内存

void showClassList()
{
	cout << "共有内存块:" << myclasslist.size() << endl;
	for (auto ib = myclasslist.begin(), ie = myclasslist.end(); ib != ie; ib++)
	{
		cout << "内存地址:"<<ib->p <<"   个内存个数:"<<ib->n<< endl;
	}
}
class myclass
{
public:
	char ch;
	//int i;
	myclass()
	{

		cout << "create myclass" << endl;
	}
	~myclass()
	{
		cout << "~delete myclass" << endl;
	}

	void show()
	{
		MessageBoxA(0, "hello", "bb", 0);
	}
	void print()
	{

		//int i = 0;
		cout << "abcd" << endl;
	}

	void * operator new(size_t size)
	{
		cout << "size:" << size << endl;
		//如果引用全局的New  会调用二次构造函数。
		//如果我们使用malloc() 跳过全局的 new 就只调用一次构造函数。
		//void *p = malloc(sizeof(myclass));  //注意传递过来的是个数。不能这样写
		void *p = malloc(size);
		if (p != nullptr)
		{
			//管理内存
			info infonow;
			infonow.p = (myclass *)p;		//创建了就记录一下。
			//infonow.n = 1;
			if (size / sizeof(myclass) == 1)	//说明只有一个对象
			{
				infonow.n = 1;
			}
			else
			{
				//size-4:由于不是一个对象,所以要用数组来保,数组地址占4个字节
				//所以要减去 4 然后再除以 对象所占有的字节数,就可以获得有多少个
				//数组对象了。
				infonow.n = (size - 4) / sizeof(myclass);
			}

			myclasslist.push_back(infonow);		//插入链表

			return p;
		}
		
	}
	void * operator new[](size_t size)
	{
		cout << "size[]:" << size << endl;
		return operator new(size);	//回调 new
	}
	void operator delete (void *p)
	{
		//删除时需要检测一下有没有这片内存。
		for (auto ib = myclasslist.begin(), ie = myclasslist.end(); ib != ie; ib++)
		{
			//如果查询到,则删除、并释放内存。
			if (p == (*ib).p)
			{
				myclasslist.erase(ib);	//删除
				free(p);	//释放。
				p = nullptr;
				break;
			}
		}
	}
	void operator delete[](void *p)
	{
		operator delete(p);	//回调 delete
	}


};

void main()
{
	myclass *p1 = new myclass;
	myclass *p2 = new myclass;
	myclass *p3 = new myclass;
	myclass *p4 = new myclass;
	myclass *p5 = new myclass;
	cout << "myclass_sizeof:" << sizeof(myclass) << endl;
	myclass *p6 = new myclass[10];
	//myclass *p7 = new myclass[10];
	delete p1;
	delete p1;

	//delete[]p6;
	cout << endl;

	showClassList();

	//调用函数,注意数组对象,因为前面有一个4个字节标识数组名,所以
	//如果是数组的则要跳过 4 个字节的数组名。
	for (auto i:myclasslist)
	{
		if (i.n == 1)
		{
			i.p->show();
		}
		else
		{
			for (int j = 0; j < i.n; j++)
			{	//为什么要多 j+1 则不是 j 开始,因为数组第一个存储的是数组地址。
				(i.p[j + 1]).show();
			}
		}
	}

	cin.get();
}



/*
 下面的代码是演示nullptr 的对象 引用 类函数的代码。
*/

类:函数是共享的,数据是每个对象私有的
空指针调用成员函数,没有访问数据可以调用,如果访问了数据不可以调用。
//class myclass
//{
//public:
//	void show()
//	{
//		MessageBoxA(0, "hello", "bb", 0);
//	}
//	void print()
//	{
//		int i = 0;
//		cout << "abcd" << endl;
//	}
//};
//
//void main11A()
//{
//	/*因为代码区是共享的(sizeof 没有统计代码区)
//	只要代码区没有引用外部的变量,这样使用就不会出问题。
//	*/
//	myclass *p(nullptr);		
//	p->show();	//这样也可以访问....
//	
//	/*
//	下面调用不成功,因为 函数中有个变量 i,而空指针没有分配内存,
//	则 i 不存在。
//	*/
//	//p->print();
//
//
//
//	cin.get();
//}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值