Hacker News new | ask | show | jobs
by activepeanut 4944 days 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 []".

1 comments

Why does _data rely on _size being initialized first? It uses "size" not "_size" in the expression.
Good catch. You're correct. I wish I could delete my original comment.