Jump to content
  • Advertisement

Archived

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

wah_on_2

how to create array of object in c++?

This topic is 6037 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

Like that: class A{ int num; //data member A(int i) //constructor { num = i; } } how to create array of A object? Thanks

Share this post


Link to post
Share on other sites
Advertisement

A* objArray = new A[NumObjects];

note that only the default constructor is called for these new objects.

--- krez (krezisback@aol.com)

Edited by - krez on January 3, 2002 2:17:52 PM

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!