16 ms·
template<typename T> Array<T>::Array(int size) : _size(size), _data(new T(size)) // should have been new T[size] {} That's not the only thing wron
by activepeanut 14y ago
template<typename T>
Array<T>::Array(int size)
: _size(size),
_data(new T(size)) // should have been new T[size]
{}
That's not the only thing wrong here.
private:
T* _data;
int _size;
};
The order of initialization is the order of declaration, NOT the order you use in your constructor.
_data depends on _size being initialized first. Therefore _size must be declared above _data.
Note: I understand the next example goes over this issue. I just think this example should've been declared properly as to avoid distracting the reader from the main problem, "() vs []".
- aidenn0 14y agoWhy does _data rely on _size being initialized first? It uses "size" not "_size" in the expression.
- activepeanut 14y agoGood catch. You're correct. I wish I could delete my original comment.