随笔 - 13, 文章 - 18, 评论 - 18, 引用 - 0
数据加载中……

运算符重载

operator +
operator ++
 
For example
 
RMB operator + (RMB& s1, RMB& s2)
{
  unsigned int yuan = s1.yuan+s2.yuan;
  RMB result(yuan);
  return result;
}
 
RMB& operator ++ (RMB& s )
{
   s.yuan++;
   return s;
}
 
区别:  值返回与引用返回
 
 
运算符做成员函数的方式
 
RMB RMB::operator + (RMB& s)
{
  int nIndex = m_nIndex + s.m_nIndex;
  RMB temp(nIndex);
  return temp;
}
 
RMB& RMB::operator ++()  //前++
{
   m_nIndex++ ;
   return *this;
}
 
RMB RMB::operator ++(int) //后++
{
  RMB temp(*this);
  m_nIndex++;
  return temp;
}
 
RMB& RMB::operator = (RMB& s)
{
  m_nIndex = s.m_nIndex;
  return *this;
}

posted on 2007-02-03 10:26 JackLi 阅读(206) 评论(0)  编辑 收藏 引用 所属分类: Programming Tips


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理