C++沉思录 句柄类里的一个函数不太理解 附上代码 求解释

小鸟向前飞 2013-04-20 10:23:16


// 2013041701.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

// 实体类
class Point{
public:
Point():xVal(0), yVal(0){}
Point(int x, int y):xVal(x), yVal(y){}
int x() const {return xVal;}
int y() const {return yVal;}
Point& x(int xv){xVal = xv; return *this;}
Point& y(int yv){yVal = yv; return *this;}

private:
int xVal;
int yVal;
};

// 计数器类
class UseCount{
public:
UseCount();
UseCount(const UseCount&);
UseCount& operator=(const UseCount&);
~UseCount();

bool only();
bool reattach(const UseCount&);
bool make_only();
private:
int *p;
};

UseCount::UseCount():p(new int(1))
{
}

UseCount::UseCount(const UseCount& other):p(other.p)
{
++*p;
}

UseCount::~UseCount()
{
if(--*p==0)
delete p;
}

bool UseCount::only()
{
return *p==1;
}
bool UseCount::reattach(const UseCount& other)
{
// 先递增右边
++*other.p;
// 判断左边
if(--*p==0)
{
delete p;
p = other.p;
return true;
}
p = other.p;
return false;
}
bool UseCount::make_only()
{
if(*p==1)
return false;
--*p;
p = new int(1);
return true;
}

// 定义句柄类
class Handle{
public:
Handle();
Handle(int xv, int yv);
Handle(const Point& p0);
Handle(const Handle& h);
Handle& operator=(const Handle& other);
~Handle();
int x() const;
int y() const;
Handle& x(int xv);
Handle& y(int yv);

private:
Point *p;
UseCount u;
};

Handle::Handle():p(new Point)
{
}

Handle::Handle(int xv, int yv):p(new Point(xv, yv))
{
}

Handle::Handle(const Point& p0):p(new Point(p0))
{
}

Handle::Handle(const Handle& h):u(h.u),p(h.p)
{
}

Handle::~Handle()
{
if(u.only())
delete p;
}

Handle& Handle::operator=(const Handle& h)
{
if(u.reattach(h.u))
delete p;
p = h.p;
return *this;
}

int Handle::x() const
{
return p->x();
}

int Handle::y() const
{
return p->y();
}

Handle& Handle::x(int xv)
{
/**
if(*p==1)
return false;
--*p;
p = new int(1);
return true;
*/
if(u.make_only())
p = new Point(*p); // p是point
p->x(xv);
return *this;
}

Handle& Handle::y(int y0)
{
if (u.make_only()) // 就是这里 为什么用先用make_only 然后p=new Point(*p)?p和*p指的是一个吗
p=new Point(*p); // 这一句和上一句
p->y(y0);
return *this;
}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}



Handle& Handle::y(int y0)
{
if (u.make_only()) // 就是这里 为什么用先用make_only 然后p=new Point(*p)?p和*p指的是一个吗
p=new Point(*p); // 这一句和上一句
p->y(y0);
return *this;
}

就是这里不理解 这个一个是这个meke_only是干什么用的 在一个是p=new Point(*p)这个*p怎么理解?
...全文
99 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahui5252 2013-04-21
  • 打赏
  • 举报
回复
错了。是生成一个新的Point.并复制值。不过有点不解。point并没有对应的构造。试了下加上 Point(Point &p) { xVal = 3; yVal = 3;}p=new Point(*p); 调用的是这个构造。那 没有的话估计编译器会自动生成
ahui5252 2013-04-21
  • 打赏
  • 举报
回复
这是一种惰性思想吧。开始所有的指向同一对像。只有当某个要改变时,才生成一个新的对像。 make_only首先判断是否唯一,是就直接返回失败。因为该对像本来就只有一个用户。可以直接改内容。 否的话把原计数减一然后new一个新计数并初始为1.并返回true表示要生成一个唯一用户 然后x,y两个函数里会重新生成一个point对像。并始用拷贝构造Handle(const Point& p0); 复制原指向对像。然后再给x,y 配新值。这样一个新的对像生成。原来的对像还在。指向原来对像的指针也无影响。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧