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.

woodenToaster
  • 264
  • 1
  • 4
  • 13
  • You don't seem to need an array, why do you want one? – K-ballo Feb 13 '13 at 02: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. – woodenToaster Feb 13 '13 at 02:50
  • Nothing in there spells _array_ though... – K-ballo Feb 13 '13 at 02:55
  • You can never create a pointer *to a class*. – Ed S. Feb 13 '13 at 02: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." – woodenToaster Feb 13 '13 at 03:00
  • You're right Ed. I should have said "object" or "instance of a class." thanks. – woodenToaster Feb 13 '13 at 03:01
  • http://stackoverflow.com/questions/6212206/creating-array-of-pointers-to-class-object, http://stackoverflow.com/questions/5982633/c-pointer-arrays, http://stackoverflow.com/questions/5887615/creating-an-array-of-object-pointers-c, http://stackoverflow.com/questions/6498039/how-to-create-array-of-pointers-in-c – jogojapan Feb 13 '13 at 03:12

2 Answers2

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

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
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.

Community
  • 1
  • 1
JaredC
  • 5,150
  • 1
  • 20
  • 45
  • 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 Feb 13 '13 at 03:26
  • @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. – JaredC Feb 13 '13 at 04:33