创建指向另一个类的指针数组的语法是什么?

时间:2021-07-04 22:30:01

If I have a class with a private variable that's supposed to hold an array of pointers to another class, is the following syntax correct?

如果我有一个带有私有变量的类,它应该包含指向另一个类的指针数组,那么以下语法是否正确?

class MyClass
{
    private:
        int arraySize;
        SomeOtherClass* classPtr[];
}

Later, when I want to dynamically allocate memory for this array in a function in MyClass that accepts an ifstream, reads from a file, and fills the array, would I do it like this?

后来,当我想在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);
    }

Thanks for your time in advance.

感谢您提前的时间。

2 个解决方案

#1


2  

That's incorrect. You cannot yet use variable length arrays in C++

那是不对的。您还不能在C ++中使用可变长度数组

That should be

那应该是

SomeOtherClass** classPtr;

And in createArray()

在createArray()中

...
classPtr = new SomeOtherClass*[arraySize];
...

And yes, forget everything I said and use std::vector

是的,忘记我说的一切,并使用std :: vector

#2


0  

Arrays must have a static size at compile time, but in your case you don't know the size until you read it from the stream at runtime. For a dynamically sized array, you should use a vector instead:

数组在编译时必须具有静态大小,但在您的情况下,在运行时从流中读取它之前,您不知道大小。对于动态大小的数组,您应该使用向量:

std::vector<SomeOtherClass*> classPtr;

Then in your code below:

然后在下面的代码中:

classPtr.reserve(arraySize);
.....
classPtr.push_back(new SomeOtherClass(...));

This being said, using raw pointers in a vector is not advised and should be done with care. Read this answer to learn more about it and whether its right for you. More likely than not, you just want a vector of your objects:

这就是说,不建议在向量中使用原始指针,应谨慎使用。阅读此答案以了解有关它的更多信息以及它是否适合您。更有可能的是,你只需要一个对象的矢量:

std::vector<SomeOtherClass> vec;
.....
vec.push_back(SomeOtherClass(...));

When using a vector of objects you should be sure to think about the move/copy semantics of your object and make sure that its right for your use case.

使用对象向量时,您应该确保考虑对象的移动/复制语义,并确保它适合您的用例。

#1


2  

That's incorrect. You cannot yet use variable length arrays in C++

那是不对的。您还不能在C ++中使用可变长度数组

That should be

那应该是

SomeOtherClass** classPtr;

And in createArray()

在createArray()中

...
classPtr = new SomeOtherClass*[arraySize];
...

And yes, forget everything I said and use std::vector

是的,忘记我说的一切,并使用std :: vector

#2


0  

Arrays must have a static size at compile time, but in your case you don't know the size until you read it from the stream at runtime. For a dynamically sized array, you should use a vector instead:

数组在编译时必须具有静态大小,但在您的情况下,在运行时从流中读取它之前,您不知道大小。对于动态大小的数组,您应该使用向量:

std::vector<SomeOtherClass*> classPtr;

Then in your code below:

然后在下面的代码中:

classPtr.reserve(arraySize);
.....
classPtr.push_back(new SomeOtherClass(...));

This being said, using raw pointers in a vector is not advised and should be done with care. Read this answer to learn more about it and whether its right for you. More likely than not, you just want a vector of your objects:

这就是说,不建议在向量中使用原始指针,应谨慎使用。阅读此答案以了解有关它的更多信息以及它是否适合您。更有可能的是,你只需要一个对象的矢量:

std::vector<SomeOtherClass> vec;
.....
vec.push_back(SomeOtherClass(...));

When using a vector of objects you should be sure to think about the move/copy semantics of your object and make sure that its right for your use case.

使用对象向量时,您应该确保考虑对象的移动/复制语义,并确保它适合您的用例。