aboutsummaryrefslogtreecommitdiff
path: root/cpp07/ex02/Array.hpp
blob: fec8698626aa1acfcc5ede5686d203190ea4736f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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