cppTest-7.6:重载new&delete

/**
 * cppTest-7.6:重载new&delete
 *
 *1、在类里面定义的重载new、delete只能对该类起作用
 *2、new、delete都是类的静态成员,而且是自动成为静态成员,无需程序员手动声明。
 *		所以不能访问类的非静态成员!
 *3、new和delete应该成对出现,即都重载或都全局。但是不这样做也不会出错。
 *4、new在对象创建之前被调用,delete在对象销毁后调用,即:
 *		先new出空间再调用构造函数:
 *		Class *c=new Class(...);<=>Class *c=Class::operator new (sizeof(Class));Class::Class(c,...);
 *		先调用析构函数再delete掉空间:
 *		Class::~Class(c);Class::operator delete(c,sizeof(Class));
 * author 炜sama
 */
#include<iostream.h>
#include<stdlib.h>
class three_d{
private:
	int a;
	static int si;
public:
	int x,y,z;
	three_d(int a,int b,int c){x=a;y=b;z=c;}

	//必须返回void*。
	//size_t是stdlib.h里定义的:typedef unsigned int size_t;,
	//下面调用p=new three_d(1,2,3);时自动将size_t初始化为sizeof(three_d)。
	//其实size_t替换为unsigned int也没错,但是不能这么做!因为环境不同,size_t的定义可能就不同了!
	void *operator new(size_t size)
	{
		//cout<<a<<endl;//不能访问非静态变量a!delete中也是这样!
		cout<<"new时打印静态成员si:"<<si<<endl;
		cout<<"类的重载new"<<endl;
		return malloc(size);
	}
	//返回必须是void
	//第一个参数是void *
	void operator delete(void *p){cout<<"类的重载delete"<<endl;free(p);}
	//void operator delete(void *p,size_t size){cout<<"类的重载delete"<<endl;free(p);}

	void print(){cout<<x<<","<<y<<","<<z<<endl;}
};
int three_d::si=100;

ostream& operator<<(ostream& stream,three_d obj)
{
	stream<<obj.x<<",";
	stream<<obj.y<<",";
	stream<<obj.z;
	return stream;
}
void main()
{
	three_d *p,*p1;

	p=new three_d(1,2,3);//这里调用的是类的重载new
	if(!p){
		cout<<"new p失败!"<<endl;
		return;
	}
	cout<<*p<<endl;
	p->print();
	delete p;//同上


	p1=::new three_d(4,5,6);//::是全局域解释操作符,即使类重载了new,一样会调用全局的new!
	if(!p1){
		cout<<"new p1失败!"<<endl;
		return;
	}
	cout<<*p1<<endl;
	p1->print();
	::delete p1;//同上

	int *pt=new int;//重载new只对操作的类有用,对于普通的类型调用的是全局的new!
	if(!pt){
		cout<<"new pt失败!"<<endl;
		return;
	}
	*pt=10;
	cout<<*pt<<endl;
	delete pt;//同上
}

这个错误的原因是在运行"./Log4cppTest"时,系统无法找到共享库"liblog4cpp.so.5",导致无法打开共享对象文件。 解决这个问题的方法是确保"liblog4cpp.so.5"库文件存在于系统的共享库路径中,并且路径已正确配置。您可以通过以下步骤来解决这个问题: 1. 首先,确认您已经安装了"liblog4cpp.so.5"库文件。您可以在终端中使用以下命令来查找该文件: ``` $ find / -name liblog4cpp.so.5 ``` 如果该文件存在,则会返回文件的路径。如果不存在,您需要下载并安装该库文件。 2. 一旦确认库文件存在,您需要将其路径添加到系统的共享库路径中。可以通过以下命令将库文件路径添加到/etc/ld.so.conf文件中: ``` $ sudo echo "/path/to/liblog4cpp.so.5" >> /etc/ld.so.conf ``` 请将"/path/to/liblog4cpp.so.5"替换为实际的库文件路径。 3. 更新共享库缓存,使系统能够找到新添加的库文件路径: ``` $ sudo ldconfig ``` 现在,您应该可以成功运行"./Log4cppTest",而不再出现"error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file or directory"的错误信息了。请按照上述步骤进行操作,确保路径和命令的准确性。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [VS code出现错误:g++.exe: error: Hello: No such file or directory g++.exe: error: Word.cpp: No such ...](https://blog.csdn.net/qq_45708377/article/details/112732294)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [cpptest:C++学习笔记 包括 侯捷讲解的系列CPP课程以及UNP、APUE等](https://download.csdn.net/download/weixin_42122306/19205361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值