现在的位置: 首页 > 综合 > 正文

C++ Primer 笔记–13章:定义值型类

2013年06月05日 ⁄ 综合 ⁄ 共 1125字 ⁄ 字号 评论关闭

要使指针成员表现得像一个值,复制 HasPtr 对象时必须复制指针所指向的对象。
复制构造函数不再复制指针,它将分配一个新的 int 对象,并初始化该对象以保存与被复制对象相同的值。每个对象都保存属于自己的 int 值的不同副本。因为每个对象保存自己的副本,所以析构函数将无条件删除指针。

赋值操作符不需要分配新对象,它只是必须记得给其指针所指向的对象赋新值,而不是给指针本身赋值。

/*
 * Valuelike behavior even though HasPtr has a pointer:
 * Each time we copy a HasPtr object, we make a new copy of the underlying int object to which ptr points
 */
class HasPtr
{
public:
	//no point to passing a pointer if we're going to copy it anyway
	//store pointer to a copy of the object we're given
	HasPtr(const int &p, int i):ptr(new int(p)), val(i) {}

	//copy members and increment the use count
	HasPtr(const HasPtr &orig):
		ptr(new int(*orig.ptr)), val(orig.val) {}
	HasPtr& operator=(const HasPtr &);
	~HasPtr() { delete ptr; }

	//accessors must change to fetch value from Ptr object
	int get_ptr_val() const { return *ptr; }
	int get_int() const { return val; }

	//change the appropriate data member
	void set_ptr(int *p) { ptr = p; }
	void set_int(int i) { val = i; }

	//return or change the value pointed to, so ok for const objects
	int *get_ptr() const { return ptr; }
	void  set_ptr_val(int p) const { *ptr = p; }

private:
	int *ptr;
	int val;
};

HasPtr& HasPtr::operator=(const HasPtr &rhs)
{
	//Note:Every HasPtr is guaranteed to point an actual int ; We know that ptr cannot be a zero pointer
	*ptr = *rhs.ptr;
	val = rhs.val;
	return *this;
}

抱歉!评论已关闭.