Jump to content
  • Advertisement

Archived

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

shrooboo

dynamic objects in c++

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

I am going to have objects in my program that are defined by a class. One problem is that I want to be able to create more objects while the program is running. I also need to perform tasks on each object. Can anyone help me out or point me to some tutorials on creating and managing objects while running? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Look into dynamic memory allocation: Pointers, dynamic arrays, linked lists and so forth; keywords new and delete ... If you already know how to handle this sort of thing, it''s no different with objects than with primitive data types.

Share this post


Link to post
Share on other sites
hrmm. I see what you are saying, but how would I go through a list of dynamic objects (all derived from the same class) and check/modify each one?

What I am trying to do is create 3D objects from a class. Once an object is created I define its verticies. Then I want to be able to check If it has collided with any other objects.

Share this post


Link to post
Share on other sites
How much experience do you have with c++ ?

What you're asking is basic to any programming language really.

[edited by - daerid on November 5, 2002 6:13:20 PM]

Share this post


Link to post
Share on other sites
I have good experience, but I was wondering if there was a better, more advanced way then with just allocating memory for a larger array every time I want to add an element. (other languages are more array friendly with dynamically resizing them, so I just wondered if there was a class specific way of doing what I wanted to do.)

[edited by - shrooboo on November 5, 2002 10:06:46 PM]

Share this post


Link to post
Share on other sites
Well, arrays, by their very nature, are slow and cumbersome to resize (STL vectors are, as I understand, pretty much as good as they come, but I doubt even those are blazingly fast when they have to grow); if that concerns you (and especially if you don''t need random access), just use a linked list or some other, more appropriate data structure.

If you truly have "good experience" with C++, whipping up a linked list class should be a snap. (If, for some reason, you don''t want to use std::list ...)

Share this post


Link to post
Share on other sites
I could use linked lists, but then I would need a unique name for each object I create. That would require the equivalent of Javacript''s eval() (I think thats the one) function, where It takes a string and converts it to the name of a variable or whatever.

Ill look into std:list

Share this post


Link to post
Share on other sites
quote:
Original post by shrooboo
I could use linked lists, but then I would need a unique name for each object I create.

No, you wouldn''t.

Share this post


Link to post
Share on other sites
Like Oluseyi said, you don''t need a specific name or identifying property for each element in order to store and manipulate them. On the downside, however, all you have is a bunch of ambiguous objects that are all of the same type, and unless you do have some sort of identification mechanism, you won''t know how to work with the first item or the fiftieth item, assuming each item is supposed to be unique from every other item and be manipulated seperatly. An example of this would be a database program (a simple one at that), where all your records are stored in a list. If the user wishes to change the age of "Robert Smith" to "27," then you need to able to identify each record, in this case by name. An example of a usage that wouldn''t necessarily require identification is a list of particles for a particle engine. Each one in and off itself in unimportant - you simply add each particle with specific properties, and the sytem automates the behavior by calling the same Process function on all of them. You don''t necessarily need to distinguish between particle 45 and particle 874.

Share this post


Link to post
Share on other sites
zipster: your example doesn''t make sense. for one thing it''s unlikely you would specifically want to change someone''s age. if you did want to then it would be because you knew what their birthdate was and that the age value hadn''t been updated since their last birthday. the kind of process you would use to determine whether they needed updating probably would mean...
quote:
...the sytem automates the behavior by calling the same Process function on all of them


but you are right in that it''s tempting to have everything derived from one base class and put in one container. then you can''t do much useful work with them because you have to determine what they are before using them.

i think it''s better to have seperate containers which hold poniters to objects according to the useful interface, ie the one you will use automatically on all the objects in the container.

in the past i''ve created objects, put them straight into a container and later deleted them from that container. that''s fine in terms of general memory management. but i haven''t gone the next stage of also putting a pointer to that object into a useful container. i ended up having lots of casts, which, as i indicate, could be avoided.

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!