C++:通过对象自动管理内存,往智能指针靠近,再实现部分运算符重载

前言

设计一个模仿整型的类来引出下文

class Int
{
private:
	int value;
public:
	Int(int x = 0) : value(x)
	{
		cout << "Create Int: " << this << endl;
	}

	Int(const Int& it) : value(it.value)
	{
		cout << "Copy Create Int: " << this << endl;
	}
	Int& operator =(const Int& it)
	{
		if (this != &it)
		{
			value = it.value;
		}
		cout << this << " = " << &it << endl;
		return *this;
	}

	~Int() { cout << "Destroy Int: " << this << endl; }
};

正常指针

在正常申请堆区空间时,我们必须要通过free或 delete释放空间,比较麻烦

int main(void)
{
	Int* ip = new Int(10);

	delete ip;
	return 0;
}

设计的类声明的指针

class Object
{
	Int* ip;
public:
	Object(Int* s = NULL) : ip(s)
	{
		cout << "Create Object: " << this << endl;
	}
	~Object()
	{
		if (ip != NULL)
		{
			delete ip;
		}
		ip = NULL;
		cout << "Destroy Object: " << this << endl;
	}
};

int main(void)
{
	Object obj(new Int(10));
	return 0;
}

通过设计这样一个类,可以让他自动释放堆区的空间,不需要再频繁地写delete或free了。

那么问题就是,如果想要取Int类型的值,如何办到?
首先在Int类里写两个取值的方法

	int& Value() { return value; }
	const int& Value()const { return value; }

然后在 Object 类型中实现指向运算符和解引用运算符的重载:

	Int& operator*()
	{
		return *ip;
	}
	const Int& operator*() const
	{
		return *ip;
	}

	Int* operator->()
	{
		return &**this;
		//return ip;
	}
	const Int* operator->() const
	{
		return &**this;
		//return ip;
	}

这样就可以可以访问Int的值:

int main(void)
{
	Object obj(new Int(10));
	(*obj).Value();
	obj->Value();

	return 0;
}

解释:
*ip :ip存放 Int的地址,那 *ip就是 Int本身,所以对Object类型解引用就是 Int本身,可以通过(.)运算符来访问Int的成员。

&**this: this表示 Object的对象,*this为 对象本身,**this表示调用 该对象的 operator *(),该运算符返回 *ip, 再取地址(&),即 ip本身的地址。

总之,我们使用一个对象管理指针,和拿一个裸指针管理一个指针,是有一定的好处的,实现内存的自动化管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_索伦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值