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;
}