Sign in to follow this  
GotenRulezU

Adding objects to a vector within the constructor.

Recommended Posts

Is there a way to do this? When I call a constructor of a class i want it to push_back into a premade vector. Is there a way to do this? I tried to use graphics.push_back(this); but it didn't work. I specified my vector by vector<myGraphics> graphics; myGraphics is the class i was trying to push back in the constructor. Am i making sense? Is there a way to do this? Thanks,

Share this post


Link to post
Share on other sites
If you define your vector to contain class objects themselves, not just pointers to class objects, then the only thing you can do in the constructor is to push a duplicate of the object onto the back of the vector (graphics.push_back(*this)). You might want to make sure the copy-constructor does what you want in this case, too.

If you make your vector a vector of pointers, however (vector<*myGraphics> graphics;), then graphics.push_back(this) will work, since this is a pointer the the current instance. This is probably more of what you're wanting, anyway. If you're not overly familiar and comfortable with pointers, however, you might need to read up on them to make sure you know precisely what's going on in memory.

Share this post


Link to post
Share on other sites
ok well even though there was no source posted ill do this:

class X
{
public:
X(std::vector<int> *graphics);//note the * if you dont have it it means you are passing the vector by value (not what you want)
int a;
};

//later
X::X(std::vector<bla> graphics):a(4)
{
graphics.push_back(a);
};

that should work if it dosent then you have to post more code

hope i helped[smile]

Share this post


Link to post
Share on other sites
what about having the vector be a static member of the class. and in the constructor you push_back(this), and you provide static accessors to the vector so that you can use that vector to iterate the objects to call update or whatever.

Share this post


Link to post
Share on other sites
Design philosophy-wise, constructors really should only, well, construct the object. IMHO, they suck anyway.

Here's one solution taking that static member idea:


class Graphics {
static vector<Graphics> registry;
// other stuff
Graphics(); // private! Implement as appropriate
public:
// possibly other stuff
// Client code that wants a graphics object calls this:
Graphics * create() { // with whatever args
registry.push_back(Graphics()); // pass the args along
// What happens: a Graphics is allocated on the stack.
// Then it is copied into place in your vector (using the
// Graphics class copy constructor (or the default one)).
// At the end of the function, the stack copy falls out of
// scope, and the one in the registry is still there.
return &*(registry.end() - 1);
// get iterator pointing to last element, deref to get the
// object, ref again to get an address (optional).
// You might prefer to return by reference; I find it easier
// to think of it this way because I basically am from a
// combined Java/C background.
}
}



This means that all the created Graphics objects will be on the heap (in the chunk of heap allocation reserved by the registry), but those objects would be there anyway. It's kind of weird to end up with *two* heap-allocated Graphics objects when you ask for one (which would happen with a naive approach - and notice that the "registered" Graphics would then be a copy of the one you asked for, not the same one!). Not to mention that you could get into problems with the copy constructor and recursion...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this