class - is there better or correct way to use objects with vector in c++? -


i have class this:

class foo{ public:    foo();     foo(int, int, int)   ~foo(); private:        int a;    int b;    int c; } 

and int main function , save elements(objects) in vector:

int main() {    vector <foo*> foo; // <------this line    for(int i=0; i<=500; i++){    foo.push_back(new foo(i+1,i+2; i+3)); //<------ line  } 

is there better solution , replace 2 line above?

tnx all;

you need unlearn java thing write "new" time create object.

int main() {    vector<foo> foo;    for(int i=0; i<=500; i++)        foo.push_back(foo(i+1, i+2, i+3)); } 

or, in c++11,

int main() {    vector<foo> foo;    for(int i=0; i<=500; i++)        foo.emplace_back(i+1, i+2, i+3); } 

Comments