C++ 默认成员函数、运算符重载 code

本文介绍了一个C++类的设计与实现细节,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载以及多种操作符如自增、比较等的重载方法。此外还展示了内存分配操作符new的重载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

holy now


#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

class deffunc
{
private:
	struct item1
	{
		int a;
	};
	struct item2 {
		struct item1 a;
	};

public:
	/*
		typedef int * pintmy;
		const pintmy ci; // equal int *const ci;
		const int * cj;
	 */

	deffunc():b(2) {a = 1; e = (struct item2 *)malloc(sizeof(struct item2)); e->a.a = 5;};
	~deffunc();
	deffunc(const deffunc &obj);
	deffunc &operator =(const deffunc &obj);
	deffunc *operator &();
	const deffunc *operator &() const;
	/*
		it is a mistake like this:
		deffunc *operator &() const;
	*/
	
	/* operator new reload */
	void *operator new(size_t size, void *addr);
	void *operator new(size_t size, char *msg);
	void *operator new[](size_t size, char *msg);
	
	/* operator ++ reload */
	deffunc &operator ++() {a++; return *this;} // ++deffuncobj
	deffunc operator ++(int){deffunc tmp(*this); ++(*this); return tmp;} //deffuncobj--
	
	struct item1 &operator *() const {return e->a;}
	/* then written like this:
	      int &operator *() const {return test.a;}
	   is a big mistake:"const int can not be converted to int &" */
	struct item1 *operator ->() const {return &(e->a);}
	
	bool operator ==(const deffunc &obj) const {return a == obj.a;}
	
	deffunc& operator +=(const deffunc &obj)
	{
		a += obj.a;
		return *this;
	}
	
	deffunc operator +(const deffunc &obj)
	{
		deffunc tmp = *this;
		return tmp += obj;
	}
	
	bool operator ()(const deffunc &obj) {return a > obj.a;}
	
	
	void setmember(int val) {a = val;}
	void print() {printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c ,d);}
	
private:
	int a;
	const int b;
	static int c;
	static const int d = 4;
	
	struct item2 *e;
	
	struct item2 test;

};

int deffunc::c = 3;

deffunc::~deffunc() {}

deffunc::deffunc(const deffunc &obj): b(obj.b)
{
	a = obj.a;
	e = (struct item2 *)malloc(sizeof(struct item2));
	e->a.a = obj.e->a.a;
}

deffunc &deffunc::operator =(const deffunc &obj)
{
	this->a = obj.a;
	return *this;
}

deffunc *deffunc::operator &()
{
	return this;
}

const deffunc *deffunc::operator &() const
{
	return this;
}

void *deffunc::operator new(size_t size, void *addr)
{
	return addr;
}

void *deffunc::operator new(size_t size, char *msg)
{
	printf("%s\n", msg);
	return ::operator new(size);
}

void *deffunc::operator new[](size_t size, char *msg)
{
	printf("%s\n", msg);
	return ::operator new(size);
}

int main()
{
	deffunc obj;
	obj.setmember(100);
	obj.print();
	
	deffunc obj1;
	obj1 = obj;
	obj1.print();
	printf("-1-------------1-\n\n");
	
	void *addr = malloc(sizeof(deffunc));
	deffunc *pobj = new(addr) deffunc;
	printf("pobj: %x, addr: %x\n", pobj, addr);
	pobj->print();
	free(addr);
	printf("-2-------------2-\n\n");
	
	deffunc *pobj1 = new("reload operator new") deffunc;
	pobj1->print();
	
	deffunc *pobj2 = new("reload operator new []") deffunc[20];
	pobj2[19].print();
	printf("-3-------------3-\n\n");
	
	obj.print();
	deffunc obj2 = ++obj;
	obj2.print();
	
	deffunc obj3 = obj2++;
	obj3.print();
	printf("obj3: %d\n", (*obj3).a);
	printf("obj3: %d\n", obj3->a);
	
	printf("-4-------------4-\n\n");
	obj2.print();
	obj3.print();
	printf("operator (): %d\n", obj2(obj3));
	
	printf("all will be OK\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值