Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::vector size or class error?

Started by ms291052 Jan 12, 2004 at 6:45 PM 6 replies 750+ views
Original Post
ms291052
ms291052
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!
Zipster
Zipster
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.
RenderTarget
RenderTarget
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.
[sub]My spoon is too big.[/sub]
Zipster
Zipster
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.
Endurion
Endurion
Since both classes get an ID of 0, you obviously have TWO instances of g_Things (or something is seriously screwing your memory up).
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Zahlman
Zahlman
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]
keeping
keeping
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)...
ms291052
ms291052
Thanks Zhalman, I''ll try that, and yes, the constructor with arguments assigns ID''s, THING() gives -1

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.