Sign in to follow this  
GMX

Array of object pointers

Recommended Posts

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;

Share this post


Link to post
Share on other sites
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[i] = new MyClass;
}

Share this post


Link to post
Share on other sites
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 works
for(int i=0; i<NUM_PTRS; i++){//if not use this
ptrs[i] = NULL;
}

MyClass* ptrs2[NUM_PTRS] = {new MyClass(2), new MyClass(15)};//this probably works too, but could be a dumb idea

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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. >_>

Share this post


Link to post
Share on other sites
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[i];


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this