dynamic objects in c++

Started by
37 comments, last by shrooboo 21 years, 5 months ago
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!
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.
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.
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]
daerid@gmail.com
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]
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 ...)
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
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.
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.
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.

This topic is closed to new replies.

Advertisement