0

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?

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.

7
  • You don't seem to need an array, why do you want one? Commented Feb 13, 2013 at 2:46
  • The short answer is, because that's what the assignment calls for. The classPtr is supposed to keep track of multiple instances of SomeOtherClass. Commented Feb 13, 2013 at 2:50
  • Nothing in there spells array though... Commented Feb 13, 2013 at 2:55
  • You can never create a pointer to a class. Commented Feb 13, 2013 at 2:59
  • Maybe it's a poor assignment. It states "Create another class that maintains a private array of SomeOtherClass instances." "You will dynamically allocate the array to be of the exact size, then fill it by reading from the file." Commented Feb 13, 2013 at 3:00

2 Answers 2

2

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

That should be

SomeOtherClass** classPtr;

And in createArray()

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

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Having a vector of objects would require thinking about moving/copying objects during push_back and also if the vector has to change capacity. A vector of unique_ptr might be better.
@beldaz The referenced link provides plenty of alternatives to a vector of pointers, so I didn't want to repeat that answer. I will add a note to thinking about moving/copying though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.