CPP07 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Array.tpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Array.tpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/08 00:01:03 by kamitsui #+# #+# */
9/* Updated: 2025/08/14 18:37:37 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13/**
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.
17 */
18
19#include "Array.hpp"
20
21// --- Constructors / Destructor ---
22
23template <typename T> Array<T>::Array() : _array(NULL), _size(0) {}
24
25template <typename T> Array<T>::Array(unsigned int n) : _array(new T[n]()), _size(n) {}
26
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];
30 }
31}
32
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
36// }
37
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;
43
44 unsigned int tmp_size = this->_size;
45 this->_size = other._size;
46 other._size = tmp_size;
47}
48
49// --- Assignment Operator ---
50//
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.
54 return *this;
55}
56
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
63// delete[] _array;
64//
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];
70// }
71// }
72// return *this;
73// }
74
75template <typename T> Array<T>::~Array() { delete[] _array; }
76
77// --- Member functions ---
78
79template <typename T> T &Array<T>::operator[](unsigned int index) {
80 if (index >= _size) {
81 throw OutOfBoundsException();
82 }
83 return _array[index];
84}
85
86template <typename T> const T &Array<T>::operator[](unsigned int index) const {
87 if (index >= _size) {
88 throw OutOfBoundsException();
89 }
90 return _array[index];
91}
92
93template <typename T> unsigned int Array<T>::size() const { return _size; }
94
95// --- Exception class implementation ---
96
97template <typename T> const char *Array<T>::OutOfBoundsException::what() const throw() {
98 return "Index is out of bounds";
99}