---
title: "MFC 使用 CArray 实现二维数组"
author: "guowee"
date: "2010-01-08T14:12:01Z"
tags: []
categories: ["编程语言"]
slug: "mfc-carray"
draft: false
lastmod: "2010-01-08T14:12:01Z"
id: 565786
url: "https://guowee.iteye.com/blog/565786"
---

CArray 实现二维数组

 

在MFC中，如何既利用CArray的操作便利性，又同样实现二维数组的功能呢？下面介绍一下如何实现。

 

```
struct MapTile
{
 CString fileName;
 int tileX;
 int tileY;
 HBITMAP bitMap;
};
```

 

```
 

struct MapTileArray
{
 CArray<MapTile, MapTile&> mapTileX;

 const MapTileArray& operator=(const MapTileArray& rhs)
 {
  if(this != &rhs)
  {
   mapTileX.RemoveAll();
   mapTileX.Append(rhs.mapTileX);
   mapTileX.FreeExtra();
  }

  return *this;
 }
};

 

```

  

 

实现二维数组，必须要实现MapTileArray的 = 操作符方法。

否则就会出现下面的错误。

 

[error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'](http://www.cppblog.com/hlong/archive/2007/11/20/37015.html)

 

下面为使用时候的代码：

 

```
typedef CArray<MapTileArray,MapTileArray&> CMapTileYArray;
static CMapTileYArray mapTilesYArray;
```