读书人

怎样用类封装一个数组

发布时间: 2012-03-20 14:01:11 作者: rapoo

怎样用类封装一个数组,请指教
#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);
}

读书人网 >C++

热点推荐