1/* ************************************************************************** */
4/* Array.tpp :+: :+: :+: */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
8/* Created: 2025/08/08 00:01:03 by kamitsui #+# #+# */
9/* Updated: 2025/08/14 18:37:37 by kamitsui ### ########.fr */
11/* ************************************************************************** */
14 * @file ex02/Array.tpp
15 * @brief Implementation of the Array class template.
16 * This file is included by Array.hpp and should not be compiled directly.
21// --- Constructors / Destructor ---
23template <typename T> Array<T>::Array() : _array(NULL), _size(0) {}
25template <typename T> Array<T>::Array(unsigned int n) : _array(new T[n]()), _size(n) {}
27template <typename T> Array<T>::Array(const Array<T> &other) : _array(new T[other.size()]()), _size(other.size()) {
28 for (unsigned int i = 0; i < _size; ++i) {
29 _array[i] = other._array[i];
33// ------ Before refactor of Copy Constructor ---------
34// template <typename T> Array<T>::Array(const Array<T>& other) : _array(NULL), _size(0) {
35// *this = other; // Reuse assignment operators
38template <typename T> void Array<T>::swap(Array &other) {
39 // Replacing a member variable in its entirety
40 T *tmp_array = this->_array;
41 this->_array = other._array;
42 other._array = tmp_array;
44 unsigned int tmp_size = this->_size;
45 this->_size = other._size;
46 other._size = tmp_size;
49// --- Assignment Operator ---
51template <typename T> Array<T> &Array<T>::operator=(Array other) {
52 // Change argument to pass by value
53 swap(other); // Exchange your contents with the contents of the copy.
57// ------ Before refactor of Assignment Operator---------
58// template <typename T>
59// Array<T>& Array<T>::operator=(const Array<T>& other) {
60// // check self operator assignment
61// if (this != &other) {
62// // free exist memory
65// _size = other._size;
66// // allocate new memory and copy
67// _array = new T[_size]();
68// for (unsigned int i = 0; i < _size; ++i) {
69// _array[i] = other._array[i];
75template <typename T> Array<T>::~Array() { delete[] _array; }
77// --- Member functions ---
79template <typename T> T &Array<T>::operator[](unsigned int index) {
81 throw OutOfBoundsException();
86template <typename T> const T &Array<T>::operator[](unsigned int index) const {
88 throw OutOfBoundsException();
93template <typename T> unsigned int Array<T>::size() const { return _size; }
95// --- Exception class implementation ---
97template <typename T> const char *Array<T>::OutOfBoundsException::what() const throw() {
98 return "Index is out of bounds";