Fixed conditional jumps and added operator overload #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added default initialization of array elements in constructor taking an unsigned int :
this->_array = new T[this->size()]();
By adding parentheses at the end, the elements of the array are initialized by default, as requested in the subject. Moreover, this default initialization is essential, as failure to do so will result in a conditional jump when trying to access the array elements.
Added destructor message, and used size() method to make the code fully coherent.
Added [] operator overload for constant objects :
const T &operator[]( unsigned int index ) const
The code must allow us to access and modify the array elements of non-constant objects, but also to access the array elements of a constant object without being able to modify them. To do this, we need 2 overloads of the [] operator (one for non-constant objects, and one for constant objects).
Removed _size(src.size()) in copy constructor :
The copy constructor calls the assignment operator, which itself contains a line for copying the value of the _size attribute. Moreover, there's a condition in the assignment operator that says to copy this value only if it's not equal to 0. However, if you initialize _size directly in the copy constructor with the _size value of the src object, before calling the assignment operator, and the value copied into _size is equal to 0, you're doing something you wanted to prevent by putting the condition present in the assignment operator.