Jump to content
  • Advertisement

Archived

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

ms291052

std::vector size or class error?

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

Right now I''m using a base class, THING, and several derived classes in my app. I''m storing everything in a vector<thING*> called g_Things. To reference a thing I use a DWORD ID that is generated in the THING constructor like so: THING::THING(...) { ID = g_Things.size(); g_Things.push_back(this); ... } or, in my ACTOR : public THING class, ACTOR::ACTOR(...) { ID = g_Things.size(); g_Things.push_back(this); ... } which is the same thing. The problem that I''m having is that upon initialization everything works fine, except two THINGs (namely my level THING and my player ACTOR) both get set to ID''s of 0. Is this just something weird I''m doing wrong with using size? Or might it have something to do with the fact that I''m using multiple extern''s to that vector or something? Any ideas? If you need to see more code, I''d be happy to share. Thanks for your time and help!

Share this post


Link to post
Share on other sites
Advertisement
Why are you calling the same code in the derived class constructor? The base class constructor is called and takes care of that. That''s probably what''s going on.

Share this post


Link to post
Share on other sites
And how would calling it twice give him the same result both times? I believe that''s the question he''s asking.

I don''t know offhand; I''d have to see some code.

I like pie.

Share this post


Link to post
Share on other sites
His question is somewhat ambiguous. I don''t know if he means two of the elements in the vector have the same ID, or two separate instances have the same ID. Because if it''s the former, that would be caused by passing the same pointer twice to the vector, thus the same instance twice, both of which obviously have the same index.

And besides, the code he has is incorrect. Just thought I''d point it out.

Share this post


Link to post
Share on other sites
Also, don't use something slow and indirect like .size() to generate IDs. I assume you only need the IDs to be unique, in which case this should suffice:

class Thing {
static int idgen = 0;
// your intention is a lot clearer this way. Also, if something
// is deleted and then you add a new Thing, you won't end up
// with duplicate IDs.
int id;
static Vector<Thing *> g_Things = new Vector<Thing *>;
Thing() {
id = idgen++;
g_Things.push_back(this);
}
}

class Actor : public Thing {
// Don't repeat that constructor stuff in here!
// If you need to do more stuff to construct an actor, do something like:
Actor() {
doMyStuff();
super();
}
}


[edited by - Zahlman on January 13, 2004 4:39:37 AM]

[edited by - Zahlman on January 13, 2004 4:40:07 AM]

Share this post


Link to post
Share on other sites
by "THING::THING(...)", I am assuming that you have a constructor with arguments. Could it be (by some chance) you are using the default constructor (THING()) which does not assign the ID? Heh heh..I am saying this, because this happened to me once (or twice)...

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!