Jump to content
  • Advertisement

Archived

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

chbfiv

Dyn class/object creation

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

Maybe someone can give me some other methods that might be dynamic instead of static for basic class creation.
#define MAX_OBJECTS 1000

class Foo {
public:
    Foo() {
        for(int i = 0;i < MAX_OBJECTS;i++)
            bars[i] = NULL;
    }
    ~Foo() {
         for(int i = 0;i < MAX_OBJECTS;i++) {
             if(bars[i])
                 delete bars[i]
         }
    }
    Bar *bars[MAX_OBJECTS];
    void create_bar() {
    bars[num_bars] = new Bar;
    num_bars++;
    }
private:
    int num_bars;
};

class Bar {
    int whatever;
};
I would rather have limitless objects then limited objects. [edited by - chbfiv on October 6, 2003 12:45:47 PM] [edited by - chbfiv on October 6, 2003 12:46:58 PM]

Share this post


Link to post
Share on other sites
Advertisement
A std::vector may be right up your alley.

http://www.sgi.com/tech/stl


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Dynamically allocate them instead. When you want to use more elements than exist in your previous array (array A), create a new array (array B), with slightly larger size, copy the contents of A into B, delete A, and replace A with B.

The following is valid syntax:

Bar * bars;
bars = new Bar[someintegervariable];
// (do whatever)
delete [] bars;

You could also implement a pattern such as a linked list, where each element in the list is like a link of a chain, with knowledge of the next element in addition to its own data. You can add or remove links from the chain whenever you want.

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!