如果我有一个带有私有变量的类,该变量应该包含指向另一个类的指针数组,那么以下语法是否正确?
class MyClass
{
private:
int arraySize;
SomeOtherClass* classPtr[];
}
后来,当我想在 MyClass 中接受 ifstream、从文件读取并填充数组的函数中为这个数组动态分配内存时,我会这样做吗?
void createArray(std::ifstream& fin)
{
//the first int is the array size
fin >> arraySize;
string tempString; //the file is formatted string int string int etc.
int tempInt;
classPtr[arraySize];
for(int i = 0; i < arraySize; i++)
{
fin >> tempString;
fin >> tempInt;
//assume the constructor is defined
classPtr[i] = new SomeOtherClass(tempString, tempInt);
}
提前感谢您的时间。