class Comp
{
//...
};
class MyClass
{
private:
vector<Comp*>vec;
//...
};
I need to initialize a vector of class type pointers to objects. how can I initialize it?
class Comp
{
//...
};
class MyClass
{
private:
vector<Comp*>vec;
//...
};
I need to initialize a vector of class type pointers to objects. how can I initialize it?
The vector is private
, I would have the constructor initialize it with the member initializer list:
class MyClass
{
public:
MyClass();
private:
vector<Comp*> vec;
};
MyClass::MyClass()
: vec(10, nullptr) // edit to suit the size and content.
{} // alternatively initialize it inside the body {} with loop
Use
vec.push_back(new Comp())
but remember to delete all items usingdelete vec[<item>]