diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-14 20:42:26 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-14 20:42:26 +0200 |
| commit | 7ac812044bfe771178752a52d70b18bb297c1891 (patch) | |
| tree | 5f5faf0d627304cdfe1f1f45194b3da76653fff8 /cpp07/ex02/Array.hpp | |
| parent | efea8712aaf8169b1184cceb83705ca6b8783173 (diff) | |
| download | piscine_cpp-7ac812044bfe771178752a52d70b18bb297c1891.tar.gz piscine_cpp-7ac812044bfe771178752a52d70b18bb297c1891.tar.bz2 piscine_cpp-7ac812044bfe771178752a52d70b18bb297c1891.zip | |
cpp07 done, cpp06 ex00 and ex01 start
Diffstat (limited to 'cpp07/ex02/Array.hpp')
| -rw-r--r-- | cpp07/ex02/Array.hpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/cpp07/ex02/Array.hpp b/cpp07/ex02/Array.hpp new file mode 100644 index 0000000..fec8698 --- /dev/null +++ b/cpp07/ex02/Array.hpp @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 20:14:07 by charles #+# #+# */ +/* Updated: 2020/04/14 20:41:25 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARRAP_HPP +# define ARRAP_HPP + +#include <exception> + +template<typename T> +class Array +{ +public: + Array() : m_under(new T[0]), m_size(0) + {} + + Array(unsigned int n) : m_under(new T[n]()), m_size(n) + {} + + Array(Array const& other) : m_under(new T[other.m_size]), m_size(other.m_size) + { + for (unsigned int i = 0; i < m_size; i++) + m_under[i] = other.m_under[i]; + } + + void operator=(Array const& other) + { + delete [] m_under; + m_size = other.m_size; + m_under = new T[m_size]; + for (unsigned int i = 0; i < m_size; i++) + m_under[i] = other.m_under[i]; + } + + ~Array() + { + delete [] m_under; + } + + T& operator[](unsigned int n) + { + if (n >= m_size) + throw std::exception(); + return m_under[n]; + } + + T const& operator[](unsigned int n) const + { + if (n >= m_size) + throw std::exception(); + return m_under[n]; + } + + unsigned int size() const + { + return m_size; + } + +private: + T* m_under; + unsigned int m_size; +}; + +#endif |
