Array of object pointers

Started by
7 comments, last by ToohrVyk 15 years, 9 months ago
Sad as it is that I have to ask this, how might I initialize an array containing pointers to an object? Might something like this work?
class MyClass
{
  //yadda yadda
};

MyClass *ClassArray[20] = new MyClass;
Advertisement
An array of pointer to custom classes is no different than any other array.

  • You initialize every element of the array just like you initialize every element of any other array; by initializing the elements you want, or all of them in a loop.

  • You initialize every element of the array just like you initialize a single variable of the corresponding type; by allocating memory for the pointer, or point it to some other existing object, in the case of pointers.


MyClass *ClassArray[20];for(int i = 0; i < 20; ++i){    ClassArray = new MyClass;}

Excellent. Thank you.
Quote:Original post by GMX
Sad as it is that I have to ask this, how might I initialize an array containing pointers to an object?

Might something like this work?

class MyClass{  //yadda yadda};MyClass *ClassArray[20] = new MyClass;


Long story short, yes, that works. Just be sure to zero all of them if you don't actually point them to something right away.

MyClass* ptrs[NUM_PTRS] = {NULL};//I believe this worksfor(int i=0; i<NUM_PTRS; i++){//if not use this    ptrs = NULL;}MyClass* ptrs2[NUM_PTRS] = {new MyClass(2), new MyClass(15)};//this probably works too, but could be a dumb idea
Wouldn't this work just fine too? >_> It's how I do it in my code, and it works.

MyClass *ClassArray = new ClassArray[20];
Quote:Original post by Twisol
Wouldn't this work just fine too? >_> It's how I do it in my code, and it works.

MyClass *ClassArray = new ClassArray[20];


Um, ClassArray is a type??? I'm not sure exactly what you're doing there.

He wants an array of pointers to MyClass's, not MyClass's themselves. To make the array on the heap, you do this:
MyClass** ClassPtrArray = new MyClass*[20];ClassPtrArray[8] = new MyClass();
If it's not obvious that was an utter typo, sorry. :P I meant "MyClass *ClassArray = new MyClass[20];".

I guess what I have is a pointer to an array of MyClass's, and he wants an array of pointers to MyClass's. In that case, please ignore everything I said. >_>
Be sure to zero each pointer so that you know which ones are valid and which pointers have no object assigned.

Class* classptrs[20];memset (classptrs, 0, sizeof (Class*) * 20);classptrs[3] = new Class;for (int i = 0; i < 20; i++)    delete classptrs;

Quote:Original post by thre3dee
Be sure to zero each pointer so that you know which ones are valid and which pointers have no object assigned.


What you want to do is make every pointer null. This is done by assigning the null pointer constant to every pointer, either during initialization:
MyClass *pointers[20] = {0};


Or after initialization:
std::fill(pointers, pointers + 20, static_cast<MyClass*>(0));


However, your proposed method does not null pointers, it merely sets the bit pattern of the pointers to all-zero, which is a non-portable way of making pointers null. And since it is neither simpler nor shorter than the correct approach, it should not be used.

This topic is closed to new replies.

Advertisement