constructor for class variables which are elements in vector

Hi...
I have a class with proper constructor defined. It takes a size_t argument and sets the size of an array which is a private member of the class.
Its working fine wen I create a new class variable.

Now, I want to create a vector whose elements are the class objects

vector<myclass> vec;

and assign space

vec.resize(3);

How do I call the constructor for vec[1],vec[2] etc ?

Its the same argument I need to pass to constructor all the time.

Thanks in advance..!!


1
2
3
4
5
6
7
8
9
10
for(int i = 0; i < vec.size(); i++){
    vec[i] = myclass(value);
}

// Or even easier:
vector<myclass> vec;
while(I_NEED_MORE_OBJECTS){
    vec.push_back(myback(value));
    // vector resizing in advance is redundant
}
Last edited on
Thank you..!!
Topic archived. No new replies allowed.