我想初始化一个File
对象,该对象将指针和指针指针作为参数。这是实施它的正确方法吗?使用指针和指针指向C++中的指针参数创建构造函数
//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](http://www.cplusplus.com/doc/tutorial/dynamic/)动态内存教程。 –
由于'two [k]'是'string *'类型,'s1 [k]'是'char',所以不会编译。尝试扩大为什么你需要这个论点,也许有更好的解决方案(例如'vector')。但是,如果你想删除它,你需要在每个'string *'上调用'delete',然后在'string **'上调用。 – NikolayKondratyev
@NikolayKondratyev你是对的。它应该's2 []' – Doruk