免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1598 | 回复: 0
打印 上一主题 下一主题

创建正确的构造函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-14 08:39 |只看该作者 |倒序浏览
要使用标准模板库中的容器类,必须注意元素的构造函数写法.
使用标准模板库的容器类时,要注意一点,就是虽然容器类可以存放任何对象,从简单到复杂,但是它对对象的构造函数有一定的要求.
以list容器类为例, 我们创建一个自己的列表.
example:
list  h;
这里的cell是个用户定义的类(user defined class).系统看到这一句就会为h分配内存.因为是一个填充cell class的list,所以还会间接调用cell的构造函数.由于一个类的构造函数可能有很多,所以它对cell的构造函数的形式也有一定的要求.一定要如下格式:
cell(const cell &);
list 会自动调用以上形式的构造函数,如果你没有提供 ,那么编译将无法通过.
因为这个构造函数中用到了const,因此要注意,该函数中如果用到其他函数时,其他函数的的返回值也应该是const.
以下给出所有的类,作为参考.需要强调的地方,会用下划线标出.
--------------------------------------------------------------------------
//file:pos.h
#ifndef _MAC_POS
#define _MAC_POS
#include
#include
#include
#include
namespace std {} using namespace std;
class pos
{
private:
  int x;
  int y;
public:
  pos();
  pos(int lx, int ly);
  pos(const pos & );
  pos & operator=(const pos &);     
  int getX() const;
  int getY() const;

};
#endif //_MAC_POS
--------------------------------------------------------------------------
//file:pos.cpp
#include
pos::pos():
x(0),y(0)
{
}
pos::pos(int lx,int ly):
x(lx),y(ly)
{
}
pos::pos(const pos & s)
{
x=s.getX();
y=s.getY();

}
pos & pos::operator= (const pos & s)
{
x=s.getX();
y=s.getY();
return *this;
}
int pos::getX()  const
{
return x;
}
int pos::getY() const
{
return y;
}

--------------------------------------------------------------------------
//file:cell.h
#ifndef _MAC_CELL
#define _MAC_CELL
#include
const int CELL1=0xa9b0; //┌
const int CELL2=0xa9a4; //─
const int CELL3=0xa9d0; //┬
const int CELL4=0xa9b4; //┐
const int CELL5=0xa9a6; //│
const int CELL6=0xa9c0; //├
const int CELL7=0xa9c8; //┤
const int CELL8=0xa9e0; //┼
const int CELL9=0xa9b8; //└
const int CELL10=0xa9d8;//┴
const int CELL11=0xa9bc;//┘
class cell
{
private:
  pos start;
  int width;
  int height;
  wstring content;
public:
  cell();
  
  cell(const cell &);  
  cell(int x, int y,int w,int h);
  cell(pos s,int w,int h);
  int getWidth() const;
  int getHeight() const;
  wstring getContent() const;
  pos getPos() const;
  
  ~cell();
  
  
  void setWidth(int w);
  
  void setHeight(int h);
  void setContent(wstring s);
  
};
#endif //_MAC_CELL
--------------------------------------------------------------------------
//file:cell.cpp
#include
cell::cell():
start(0,0),width(0),height(0),content()
{
}
cell::cell(int x,int y,int w,int h):
start(x,y),width(w),height(h),content()
{
}
cell::cell(const cell & c)
{
width=c.getWidth();
height=c.getHeight();
start=c.getPos();
content=c.getContent();
}
int cell::getWidth() const
{
return width;
}
int cell::getHeight() const
{
return height;
}
wstring cell::getContent() const
{
return content;
}
pos  cell::getPos() const
{
return start;
}
cell::cell(pos s,int w,int h):
start(s),width(w),height(h),content()
{

}
void cell::setWidth(int w)
{
width=w;
}
void cell::setHeight(int h)
{
height=h;
}
void cell::setContent(wstring s)
{
content=s;
}
cell::~cell()
{
}
--------------------------------------------------------------------------
//file:row.h
#ifndef _MAC_ROW
#define _MAC_ROW
#include
class row
{
private:
  list  r;  
public:
  row();
  ~row();
  void insert(cell a);
  void deletec(int pos);
  int  getSize();
};
#endif  //_MAC_ROW
--------------------------------------------------------------------------
//file:row.cpp
#include
row::row()
{
}
row::~row()
{
}
void row::insert(cell a)
{
r.push_back(a);
}
void row::deletec(int i)
{
list::iterator p;
p=r.begin();
p++;
r.erase(p);
}
int row::getSize()
{
return r.size();
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4353/showart_21246.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP