I'm doing a school project where I am to construct a custom vector class.
And the class should be able to initialize vectors in a few different ways.
I've got stuck with this initializer_list initialization of the vector.
The only values wich are allowed as elements are unsigned int.
header
#include <initializer_list>
class myvec {
private:
unsigned int *arr; //pointer to array
std::size_t n; //size of myvec
public:
myvec(); // Default contructor
myvec(std::size_t size); // Creating a vec with # of element as size
myvec(const myvec&); // Copy constructor
myvec(const initializer_list<unsigned int>& list);
cpp
#include "myvec.h"
#include <initializer_list>
myvec::myvec() {
arr = new unsigned int[0];
n = 0;
}
myvec::myvec(std::size_t size) {
arr = new unsigned int[size];
n = size;
}
myvec::myvec(const myvec& vec) {
arr = new unsigned int[vec.n];
n = vec.n;
for (int i = 0; i < vec.n; i++) {
arr[i]=vec.arr[i];
}
}
myvec::myvec(const std::initializer_list<unsigned int> list) {
}
What I don't understand is how the constructor should be written for it to work? Been trying to find answers on internet for a long time without success.
I want to call the initializer_list constructor from another c++ file as
test.cpp
myvec a = {1,2,3,4};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…