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
#include <cassert>
#include <cstddef>
#include <cstdio>

class Vector
{
    public:
        Vector(std::size_t dim);
        ~Vector();

        double &operator()(std::size_t index);
        double operator()(std::size_t index) const;

        double *data;
        std::size_t dim;
};

Vector::Vector(std::size_t dim)
    : data(new double[dim]), dim(dim)
{
}

Vector::~Vector()
{
    delete [] data;
}

double &
Vector::operator()(std::size_t index)
{
    assert(index < dim);
    return data[index];
}

double
Vector::operator()(std::size_t index) const
{
    assert(index < dim);
    return data[index];
}

void
print(const Vector &x)
{
    for (size_t i = 0; i < x.dim; ++i) {
        std::printf("%lf", x(i));
        if (i + 1 < x.dim) {
            std::printf(", ");
        }
    }
}

int
main(void)
{
    Vector x(4);

    for (std::size_t i = 0; i < x.dim; ++i) {
        x(i) = 42 + i;
    }

    print(x);
    printf("\n");
}