I have a class Vector3d with 3 double components and want to store several of them in a Field. For reasons of efficiency when sending a Field over a network via MPI, I would like to allocate contiguous memory in the Field class and let the Vector3ds store their components in that chunk of memory. In addition, Vector3ds should also be able to live without a Field; in this case, they have to care about their own memory allocation.#include <cstddef>#include <iostream>#include <vector>class Field;class Vector3d{    friend class Field;    public:        Vector3d() : components_(new double[3]) {}        ~Vector3d() { delete[] components_; }        Vector3d(const Vector3d &v) : components_(new double[3])        {            components_[0] = v.components_[0];             components_[1] = v.components_[1];             components_[2] = v.components_[2];        }        double & operator()(const std::size_t index) { return components_[index]; }    private:        Vector3d(double *ptr) : components_(ptr) {};        double *components_;};class Field{    public:            Field(std::size_t size);        ~Field();        Vector3d & operator()(const std::size_t index) { return *(vectors_[index]); }    private:        std::vector<double> components_;        std::vector<Vector3d *> vectors_;};Field::Field(std::size_t size) : components_(std::vector<double>(3 * size)), vectors_(std::vector<Vector3d *>(size, new Vector3d(0))){    double *ptr = &components_[0];    for (auto it = vectors_.begin(); it != vectors_.end(); ++it)    {        (*it)->components_ = ptr;        ptr += 3;    }} Field::~Field(){    for (auto it = vectors_.begin(); it != vectors_.end(); ++it)        (*it)->components_ = 0;}int main(int argc, char *argv[]){    Field field(2);    Vector3d &v = field(1);    v(2) = 5;    Vector3d v2 = v;    v2(2) = 2;    std::cout << field(1)(2) << std::endl;    return 0;}I would like to hear what you think about this piece of code. Can this be considered a good solution or what would you change?
Vector of vectors in contiguous memory
c++;coordinate system