Archived

This topic is now archived and is closed to further replies.

how to create array of object in c++?

This topic is 5825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is probably not the "right" way to do it, but you could have a default constructor which does nothing, and a separate Init(int) member function that you can use to set up the objects.

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
ic. I know your method can achieve the goal. Is it the one and only one solution?

Because my teacher/book teach that the function of constructor is used to initailization.

Share this post


Link to post
Share on other sites
Actually, I had a similar problem a few days ago. I was told to do something like:

  
CBlockData* blockObjects = new CBlockData* [numOfType];


But I instead did something different. If you remember, you can treat arrays similar to pointers in some respects.. here is my solution that is working for me anyways:

       
// An array of pointers, nullify it at first

CBlockData** blockObjects = 0;

// For each object...

for(int i = 0; i < numOfType; i++)
// ... Assign it

blockObjects[i] = getBlockObject(blockIndex[i]);


the call to getBlockObject is returning the address of the object and assigning it to blockObjects.

Edited by - xgalaxy on January 4, 2002 12:59:28 PM

Share this post


Link to post
Share on other sites
Remember if you are going to use the new operator, use delete somwhere down the line as well. This is vital to prevent memory leaks.

"The staff in the hand of a wizard may be more than a prop for age," -Hamá the doorward from ''''Lord of the Rings'''' by J. R. R. Tolkien

Share this post


Link to post
Share on other sites