h文件;
cpp
class ZZC
{
public:
ZZC();
virtual ~ZZC();
int& operator[] (int i); //重载"[]"操作符
private:
int myarr[6];
};
Cpp文件;
cpp
ZZC::ZZC()
{
int i;
for (i = 0; i < 6; i++)
{
myarr[i] = i + 9;
}
}
ZZC::~ZZC()
{
}
int& ZZC::operator[] (int i) //返回引用,这样才可以对返回值赋值
{
return myarr[i];
}
调用和输出;
cpp
void CZkhView::OnDraw(CDC* pDC)
{
CZkhDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str1;
ZZC zc1;
str1.Format("%d", zc1[1]);
pDC->TextOut(20, 20, str1);
zc1[4]=160;
str1.Format("%d", zc1[4]);
pDC->TextOut(20, 50, str1);
}
从这简单例子看只是提供了写法上的一些方便;
有时间继续;