Setup std::vector in class constructor

Just do:

MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0)

You already seem to know about initializer lists, why not initialize vector there directly?

vec = new vector<int>(size,0);

is illegal because new returns a pointer and in your case vec is an object.

Your second option:

vector<int> temp(size,0);
vec = temp;

although it compiles, does extra work for no gain. By the time you reach the assignment, two vectors would already have been constructed and discarded afterwards.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)