Create Instances of a Class During Runtime

Pages: 12
Aug 26, 2010 at 9:41pm
The advantage of using a vector of pointers over a vector of instances are that the pointers will provide you with the positive fact that whenever you add a "tower" you don't have to instantiate AND copy it. You instantiate and "refer" to it via a pointer. This pointer will remain validity until you delete and remove it from your vector (in that order!). The (soon to be) successor of a vector of pointers is that which Athar described: ptr_vector. It's currently only available at www.boost.org, but it will be added in a new version of C++. (This pre-release of the next version is commonly referenced to as C++0x)
Aug 26, 2010 at 10:35pm
@bolohardik:

Ok, I think I understand now. But if I did it like that, I would end up with a large amount of parameters, wouldn't I? I have been thinking. Could I just make a 'default' tower, and whenever a new one is created, just copy everything from the default into the new one? Something like this:

1
2
3
4
5
6
vector<Tower> towers;
Tower default_tower, tower;
towers.push_back(tower);
while (towers.back() != default_tower){
   towers.back() = default_tower;
}


I am not sure if I can do it this way, I just want to give an idea of what I am asking if I can do. Maybe I don't even need the while loop.

@Kyon:

So what you are saying is that it will create a new instance of Tower, then use a vector of pointers to point to that new instance of Tower, instead of storing the whole tower instance in the vector? Maybe it would be easier to do it this way, except that I have only read about pointers, and never really had a need for them with the kinds of programs I made...unless I am still not understanding correctly. If not, I can always use another method that was mentioned here.

Thanks again for the replies.
Aug 27, 2010 at 2:17am
Hi
@kyon : It was really a very nice description of difference between using vector<Class*> and vector<Class*>.Thanks a lot.

@Flaurance
vector<Tower> towers;
Tower default_tower, tower;
towers.push_back(tower);
while (towers.back() != default_tower){
towers.back() = default_tower;
}

You can do like that but instead why don't you write a default constructor like this.

class Sample
{
int s1;
int s2;
Sample()
{
s1=Default_value1;
s2=Default_value2;
}
};
int main()
{
Sample s;// or Sample s();This will automatically initialize s1 & s2 with Default_value1 and //Default_value2 as always it calls the default constructor whenever not specified.

}


use a vector of pointers to point to that new instance of Tower, instead of storing the whole tower instance in the vector

I think you have understood Kyon's point properly now and using vector<Class*> that way saves lots of memory which would have been wasted if we use vector<Class>.

Aug 27, 2010 at 2:58am
Thanks again.

@bolohardik:

I think I get your point now with the initializing. As it happens, I am probably about to make it more complicated, though...if I want the name (const char * name;) to be the same name, but with a number on the end depending on what position in the vector it is, how can this be done, inside the constructor? If I was to make a default name, once it is constructed, I wouldn't be able to change it to add the number. So is there a way to do this inside the constructor? Or would it be easier just to make it char *name;?

@Kyon:

Where exactly are the new classes stored if they are not stored in the vector itself? In a randomly placed block of memory that only the pointers knows where?

If I were to go about using this vector of pointers, should I check out the ptr_vector and use that, as Athar said:
vectors containing pointers (and owning the objects pointed to) violate RAII, so that is why you should use ptr_vector in this case.

(By the way, what is RAII?)

Thanks again for all the help.
Aug 27, 2010 at 5:52am
You should use char *name;
Topic archived. No new replies allowed.
Pages: 12