【发布时间】:2015-05-24 22:01:33
【问题描述】:
我想初始化一个File 对象,它接受一个指针和指向指针的指针作为参数。这是正确的实现方式吗?
//file.h
class File {
public: File(string * , int, string * * , int);
void clean();
private: string * one;
int size_one; //gives size of one array
string * * two;
int size_two; //gives size of *two array
}
//file.cpp
File::File(string * s1, int i1, string * * s2, int i2) {
int k;
one = new string[i1];
for (k = 0; k < i1; k++) {
one[k] = s1[k];
}
two = new string * [i2];
for (k = 0; k < i2; k++) {
two[k] = s2[k];
}
}
我应该如何实现删除两个数组的clean()函数?
【问题讨论】:
-
我建议this动态内存教程。
-
这不会编译,因为
two[k]是string*类型,而s1[k]是char。试着解释为什么你需要这个论点,也许有更好的解决方案(例如vector)。但是,如果您想删除它,您需要在每个string*上调用delete,然后在string**上调用。 -
@NikolayKondratyev 你是对的。应该是
s2[]
标签: c++ arrays pointers constructor