CPP07 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Array.hpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Array.hpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/07 23:58:23 by kamitsui #+# #+# */
9/* Updated: 2025/08/08 00:01:52 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#ifndef ARRAY_HPP
19#define ARRAY_HPP
20
21#include <cstddef> // For size_t
22#include <stdexcept> // For std::exception
23
29template <typename T> class Array {
30 private:
31 T *_array;
32 unsigned int _size;
33 void swap(Array &other); // For the Copy-and-Swap idiom
34
35 public:
36 // --- Constructor and Destructor ---
37
42
47 explicit Array(unsigned int n); // explicit to prevent implicit conversions
48
53 Array(const Array &other);
54
59
60 // --- Operators ---
61
67 Array &operator=(Array other); // Pass by value for Copy-and-Swap
68
75 T &operator[](unsigned int index);
76
83 const T &operator[](unsigned int index) const;
84
85 // --- Member functions ---
86
91 unsigned int size() const;
92
93 // --- Exception class ---
94
100 public:
105 virtual const char *what() const throw();
106 };
107};
108
109// Include the implementation file for the template class
110#include "Array.tpp"
111
112#endif
Exception thrown when trying to access an element outside the array bounds.
Definition Array.hpp:99
virtual const char * what() const
Returns the exception message.
A template class that represents a dynamic array.
Definition Array.hpp:29
T & operator[](unsigned int index)
Subscript operator for non-const access.
Array(unsigned int n)
Constructor with size. Creates an Array of n elements.
unsigned int size() const
Returns the number of elements in the Array.
Array & operator=(Array other)
Assignment operator. Replaces the contents with a copy of another Array.
const T & operator[](unsigned int index) const
Subscript operator for const access.
~Array()
Destructor. Frees the allocated memory.
Array(const Array &other)
Copy constructor. Creates a deep copy of another Array.
Array()
Default constructor. Creates an empty Array.
void printElement(const T &element)
Prints an element to the standard output.
Definition iter.hpp:65