vendredi 12 juillet 2019

How to define the size of member vector in constructor of a class?

I want to make a vector first without a size ( vector<int> times) and I want to define its size later in a constructor of a class ( times(size) ).

I can do it by using the initializer list as you see below

class A (int size): times(size) {};

But my question is that why I can not do it in a constructor out of a class like the code below?

I mean why the code below is wrong?

class A
{
public:
    A(int size);
private:
    std::vector<int> line;
};

A::A(int size)
{
    line(size);// here I got the error
}

line(size) make an error

Aucun commentaire:

Enregistrer un commentaire