请问在C++中如何实现像一下java中代码实现的功能public class Test{
int array[][] = null;
public void setValue(int a, int b){
if(array == null){
array = int[a][b];
}
} public int[] getValue(int index){
return array[index];
}
}
int array[][] = null;
public void setValue(int a, int b){
if(array == null){
array = int[a][b];
}
} public int[] getValue(int index){
return array[index];
}
}
解决方案 »
- 有没有基于Strust2的OA或人力资源方面的源代码下载
- 学ajax哪本书比较好?
- Struts1
- Spring配置问题
- Linux上报jdk版本不支持,帮我看看是什么问题啊!
- 一个Javascript和struts传值的问题.很简单.请大家帮我看看.谢谢.
- test title
- C#发送XML字符串到JAVA servlet???
- 原理讨论:关于为什么要使用PortableRemoteObject.narrow()
- % String path = request.getContextPath(); String basePath = request.getScheme()+
- spring怎么获得properties文件里面的数据?
- 关于spring 定时器的 时间问题
#include<iostream>class Test{
private:
int** array;
public:
void setValue(int, int);
int* getValue(int);
~Test();
};void Test::setValue(int a, int b){
if(array == 0){
*array = new int[a];
for(int i=0; i<a; i++) {
array[i] = new int[b];
}
}
}int* Test::getValue(int index) {
return array[index];
}
Test::~Test() {
//TODO delete array in for loop
}
int main()
{
Test t;
t.setValue(2,2);
}
class Test{
private:
int** array;
int asize;
int bsize;...void Test::setValue(int a, int b){
if(array == 0){
asize = a;
bsize = b;
*array = new int[a];
for(int i=0; i<a; i++) {
array[i] = new int[b];
}
}
}
class Test{
private:
int** array;
int len1;
int len2;
public:
Test():array(NULL){};
void setValue(int, int);
int* getValue(int);
~Test();
};void Test::setValue(int a, int b){
if(array == NULL){
int** tmp = new int*[a];
for(int i=0; i<a; i++) {
tmp[i] = new int[b];
}
array = tmp;
this->len1 = a;
this->len2 = b;
}
}int* Test::getValue(int index) {
return array[index];
}
Test::~Test() {
if(array != NULL) {
for(int i=0; i<len1; i++) {
delete[] array[i];
}
delete[] array;
}
}
int main()
{
Test t;
t.setValue(2,2);
}
for(int i=0; i<a; i++) {
tmp[i] = new int[b];
}
我要如何判断tmp[i]是否为空?
for (j=0; j<b; j++) {
if (tmp[i][j] != 0) {
...
}
}
在这段例子代码中,一开始是 tmp = new int*[a];
所以接下来的tmp[i]肯定是空,所以不用判断的