MFC的CARRAY

MFC中的CARRAY中没有重载“=”,在使用时难免会带来不便,解决的办法是创建一个类继承CARRAY,然后在该类中重载 “=”运算符。

 

#include <AFXTEMPL.H>
#include <iostream.h>

class point
{
public :
 int x,y;
 point()
 {
  x = 0;
  y = 0;
 }
 point (int x, int y)
 {
  this->x = x;
  this->y = y;
 }

 point (const point &p)
 {
  this->x = p.x;
  this->y = p.y;
 }
};


template<class TYPE, class ARG_TYPE>
class EArray : public CArray<TYPE, ARG_TYPE>
{
public:

 EArray<TYPE, ARG_TYPE> & operator = (const EArray<TYPE, ARG_TYPE> &ary)
 {
  int i, len;
  len = ary.GetSize();
  for (i = 0; i < len; i++)
  {
   this->Add(ary.GetAt(i));
  }
  return *this;
 }
};

typedef EArray<point, point&> __pointList;


__pointList * getPointList ()
{
 __pointList *pl;
 pl = new __pointList ();

 point p1(1,1), p2(2,2);
 

 pl->Add(p1);
 pl->Add(p2);

 return pl;
}

/*
__pointList & CArray<point, point&>::operator = (const __pointList &pl)
{
 int i, len;
 len = pl.GetSize();
 for (i = 0; i < len; i++)
 {
  this->Add(pl.GetAt(i));

 }
 return *this;
}*/

void getPL (__pointList **pls)
{
// pls = new __pointList ();

 (*pls)->Add(point(3, 4));
 return;
}


void main ()
{
 __pointList *pn;

 __pointList a1, a2;

 a1.Add(point(1,2));

 cout << a1.GetSize() << endl;

 a2 = a1;

 pn = new __pointList ();

 pn = getPointList();

 cout << pn->GetSize() << endl;

 cout << pn->GetAt(0).x << "  " << pn->GetAt(0).y << endl;

 getPL(&pn);

 cout << pn->GetAt(0).x << "  " << pn->GetAt(0).y << endl;

 return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值