I have a class, user, which stores objects of another class, foo, in a vector. I am trying to figure out what is the best way of creating objects of class foo and store them in vector of class user.
class Foo {
public:
Foo();
Foo(std::string str);
std::string name;
Foo* GiveMeFoo(std::string arg);
};
Foo::Foo() {}
Foo::Foo(std::string args):name(args) {}
Foo* Foo::GiveMeFoo(std::string arg) { return new Foo(arg) };
class User {
public:
vector < Foo *> v;
vector < Foo> v2;
vector < Foo*> v3;
void start();
};
void User::start()
{
const int SIZE = 3;
string a[SIZE] = {"one","two","three"};
for (int i = 0; i < SIZE; i++ ){
//method1
Foo *f = new Foo();
f->name = a[i];
v.push_back(f);
//method2
Foo f2;
f2.name = a[j];
v2.push_back(f2);
//method3
Foo f3;
v3.push_back(f3.GiveMeFoo(a[k]));
}
}
Question1: Between methods 1, 2 and 3 is there a preferred way of doing things? Is there a better way of creating objects of remote class locally and storing them in a vector?
Question 2: Are all the objects that are getting stored in the vectors of class User persistent (e.g. even if the foo object goes away, once I have pushed those objects onto vector in user, then copies of those foo objects will be persistently stored in the vector, correct?