Skip to content

Fixed conditional jumps and added operator overload #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ex02/include/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class Array
Array(): _size(0)
{
std::cout << "Default Constructor called: created empty Array of size 0" << std::endl;
this->_array = new T[this->_size];
this->_array = new T[this->size()];
// for (unsigned int i = 0; i < this->size(); i++)
// std::cout << this->_array << std::endl;
}

Array(unsigned int size): _size(size)
{
std::cout << "Constructor for an Array of size " << size << " called" << std::endl;
this->_array = new T[this->_size];
std::cout << "Constructor for an Array of size " << this->size() << " called" << std::endl;
this->_array = new T[this->size()]();
// for (unsigned int i = 0; i < this->size(); i++)
// std::cout << this->_array[i] << std::endl;
}

Array(const Array &src): _size(src.size())
Array(const Array &src)
{
std::cout << "Copy Constuctor called" << std::endl;
this->_array = NULL;
Expand All @@ -51,6 +51,7 @@ class Array
// Deconstructors
~Array()
{
std::cout << "Destructor called" << std::endl;
if (this->_array != NULL)
delete [] this->_array;
}
Expand All @@ -63,7 +64,7 @@ class Array
if (src.size() != 0)
{
this->_size = src.size();
this->_array = new T[this->_size];
this->_array = new T[this->size()];
for (unsigned int i = 0; i < this->size(); i++)
this->_array[i] = src._array[i];
}
Expand All @@ -72,7 +73,17 @@ class Array

T &operator[]( unsigned int index )
{
if (index >= this->_size || this->_array == NULL)
if (index >= this->size() || this->_array == NULL)
{
std::cout << "index: " << index << std::endl;
throw Array<T>::InvalidIndexException();
}
return (this->_array[index]);
}

const T &operator[]( unsigned int index ) const
{
if (index >= this->size() || this->_array == NULL)
{
std::cout << "index: " << index << std::endl;
throw Array<T>::InvalidIndexException();
Expand Down