首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎样用类封装一个数组

2012-03-20 
怎样用类封装一个数组,请指教#include iostreamusingnamespacestdclassexample{private:constintrowco

怎样用类封装一个数组,请指教
#include <iostream>
using   namespace   std;
class   example
{
private:
const   int   row;
const   int   col;
int   jj[row][col];
public:
example(int   i,int   j);
};
example::example(int   i,int   j):row(i),col(j)
{}
void   main()
{
example   bb(12,34);
}
应该怎样改??????

[解决办法]
#include <iostream>
using namespace std;
class example
{
private:
const int row;
const int col;
int *jj;
public:
example(int i,int j);
~example();
int &at(int i, int j)
{
return jj[i*col+j];
}
};
example::example(int i,int j):row(i),col(j)
{
jj = new int[row*col*sizeof(int)];
}
example::~example()
{
delete []jj;
}

void main()
{
example bb(12,34);
}

热点排行