Initializing classes

I have a vector of class items but when I create the item I am wondering if I can send I string or int with it.

1
2
3
4

// vector of Class Items
vector<Items*> Item;
Item.emplace_back(new Items);


so my question is can I do something like
Item.emplace_back(new Items(string str));
For non-pointers, see http://en.cppreference.com/w/cpp/container/vector/emplace_back

Pointers are simple. They have no more constructor than ints do. Therefore, emplace_back and push_back are probably quite similar for your vector.

Yes, the choice of constructor is up to you. However, syntactically:
1
2
3
4
5
Items * o = new Items; // ok
Items * p = new Items( string str ); // error
Items * q = new Items( "Hello" ); // ok
string foo { "world" };
Items * r = new Items( foo ); // ok 
Last edited on
Topic archived. No new replies allowed.