
-----------------------------------
Justin_
Wed Aug 16, 2006 3:59 am

objects and vectors.
-----------------------------------
Hmm..  Being the noob that I am, I'd like to make a vector of objects.  I've only conceptualized the following, but...  

Since the constructor of the object initializes its member variables and it is called when you initialize the vector...  


vector my_object("Lovely");


The constructor is called which takes one string as an argument.  In my case, the argument passed to the constructor dictates the rest of the member variable's initializations.  For instance if the string is "Lovely" than all the little variables become lovely too.  But what if you wanted to throw in some ugly variables?  

In my case the initializations are actually functions so there is a lot of overhead and it just wouldn't be practical to let all the values initialize and then go ahead and change them...  

Is there a way to call the constructor of each object in the array seperately?  Thanks for your time.

-----------------------------------
Justin_
Wed Aug 16, 2006 4:49 am


-----------------------------------
woops, okay so "Lovely" would actually be for the std::vector constructor.  Still, how do you construct the objects themselves?

-----------------------------------
Null
Wed Aug 16, 2006 10:04 am


-----------------------------------
This is a possible solution, but it might not be relavent to your problem.


#include 
#include 
#include 
#include 

const int SIZE = 10;

int main() {
	int elems

-----------------------------------
r.3volved
Wed Aug 16, 2006 11:04 am


-----------------------------------
I'd imagine what you're trying to do is this?

std::vector vecObjects;
obj myNewObject();
vecObjects.push_back(myNewObject);


or with pointers:

std::vector vecObjects;
obj myNewObject = new obj();
vecObjects.push_back(myNewObject);


-----------------------------------
Justin_
Wed Aug 16, 2006 12:12 pm


-----------------------------------


std::vector vecObjects;
obj myNewObject = new obj();
vecObjects.push_back(myNewObject);


Thanks guys, this one is what I was looking for.  Sorry for the noob question.

-----------------------------------
r.3volved
Wed Aug 16, 2006 7:19 pm


-----------------------------------
no noob questions, just noob lack of research ;)

keep in mind that you're using pointers there so you'll be using the -> operator unless you're dereferencing elements of your vector.
