Tomm
Tomm

Reputation: 127

Initializing a class object with the contents of a vector

I'm currently working on a matrix class and there is one part I've been stuck on.

If I wanted to initialize a class object with the contents of a vector for example

Matrix M = {1, 2, 3, 4, 5, 6}

Would I have to overload operator= or is it possible to create a constructor that can do that?

Upvotes: 0

Views: 22

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93324

If by "vector" you mean std::vector, then you can obviously create a constructor:

Matrix(const std::vector<int>&);

If you meant the {1, 2, 3, 4} syntax, then you can create a constructor taking std::initializer_list

Matrix(const std::initializer_list<int>&);

Upvotes: 2

Related Questions