Fixed conditional jumps and added operator overload#1
Open
CodeS42 wants to merge 1 commit intotblaase:mainfrom
Open
Fixed conditional jumps and added operator overload#1CodeS42 wants to merge 1 commit intotblaase:mainfrom
CodeS42 wants to merge 1 commit intotblaase:mainfrom
Conversation
…an unsigned int; added destructor message; added [] operator overload for constant objects; used size() method; removed _size(src.size()) in copy constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ) constThe 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.