Advertisement Jump to content


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


dynamic initialisation

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

Hi, before I start I know some people are gonna want to simply reply with RTFM, but I have read my (thick) fricking text book from cover to cover, and I just can''t find the solution to my problem. My code is going along well, but I have begun to hit several brickwalls because I have had to hard code some initialisations. For instance, I have a class called Rabbit, and have created two of these rabbit objects in the main.cpp file with:
Rabbit a;
Rabbit b; 
However, I want to be able to initialise an unspecified number of these objects and then reference them throughout my program. Something along the lines of:
for (i=1; i<=NumOfRabbits; i++)
Rabbit i;
and then reference each of these in the program appropriately, along the lines of:
for (i=1; i<=NumOfRabbits/2; i++)
to kill the first half of these rabbits horribly, for instance. I appreciate this code above is junk and will not work, but I have used it to illustrate what I''m trying to acheive. I can''t for the life of me work it out :''(

Share this post

Link to post
Share on other sites
Try this:

Rabbit *MyRabbits;

MyRabbits = new Rabbits[NumOfRabbits];

// Access as follows

for (int i=0; i<NumRabbits; i++)

EDIT: As VertexNormal said ... you will need to use the following to clean up after you have used the dynamically allocated memory...
delete [] MyRabbits;

[edited by - oooaah on January 12, 2004 12:57:16 PM]

Share this post

Link to post
Share on other sites
You'll need to dynamically allocate your rabbit objects using the new operator, probably as an array.

Rabbit *Rabbits;

Rabbits = new Rabbit[NumOfRabbits];

Then, you can index the Rabbits array to get access to each particular rabbit.

for (i=0; i < NumOfRabbits; ++i)
Rabbits[ i ].KillHorribly();

Remember to delete[] Rabbits; before your program exits, so you don't leave that dynamically allocated memory floating around, unusable.

EDIT: Eh, too late

vertexnormal AT linuxmail DOT org

Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.

[edited by - VertexNormal on January 12, 2004 12:26:38 PM]

Share this post

Link to post
Share on other sites
Use a vector - vector< Rabbit > RabbitVector; (without the spaces)

To add elements to a vector you can either use the push_front() or push_back() method which takes a reference to the type of object the vector is storing as a parameter - so in you example it would look something like this:

vector RabbitVector;

Rabbit ARabbit;

Now to access elements of a vector you simply use the [] notation like you would with an array (vector indicies start at 0 as well)

My 2D game engine

EDIT: changed formatting to get it to display

[edited by - Spudder on January 12, 2004 12:32:56 PM]

Share this post

Link to post
Share on other sites
What language are you using?
If its C++ and you have the standard library available then you can do this:

#include <vector>

std::vector<Rabbit> rabbits(size_of_vector, Rabbit(ctor_arg1, ctor_arg2, etc));

You can use vector in a similar fashion to arrays and you can also add (aka push) elements to them as described elsewhere in this thread.

If you would like to know more about the standard library and stl, please visit SGI's STL site or obtain this book from a bookstore or library

gl hf

[edited by - flangazor on January 12, 2004 12:44:23 PM]

Share this post

Link to post
Share on other sites
wow guys once again this community has really been great. I''m currently trying to implementing the items you have put here, thnx a lot!

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!