Overriding assignment operators in proxy class for vector object in a matrix?












1















I have a slight problem. I have a Matrix class defined as follows (in row-major form):



template<typename T>
class Matrix {
private:
class RowVector {
private:
T *_vec;
std::size_t _l;

public:
RowVector(T *vec, std::size_t l);

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

operator std::vector<T>() const;
};

std::vector<T> _data;
std::size_t _m;
std::size_t _n;

public:
Matrix(std::size_t m, size_t n, const T &elem = T());

const RowVector operator(std::size_t index) const;
RowVector operator(std::size_t index);

std::size_t getm() const;
std::size_t getn() const;
void fill(const T &elem);
void fillRow(std::size_t index, const T &elem);
void fillCol(std::size_t index, const T &elem);

Matrix &transpose(unsigned int i = 1);

const std::vector<T> &data() const;
};


and wish to overload two RowVector operators=



typename Matrix<T>::RowVector &operator=(const std::vector<T> &vec);
typename Matrix<T>::RowVector &operator=(const Matrix<T> &mat);


so I can return a RowVector & using A[0] and reassign its value using either a vector or a matrix. Keep in mind that I (presumably) can ignore the rule of three because I provide no explicit way for the client to construct a RowVector object.



However, in attempting to write the function bodies for the overloads, I have come across a problem: that



(1) I cannot copy-construct a vector/Matrix object that will persist outside of the operator='s scope so that I can assign its data() to _vec and its size() to _l.



(2) I cannot directly modify _data since it is not a static variable; even if I could, I have no way to discover the index so I can overwrite the relevant region of memory in the enclosing Matrix object.



Do you know of any way in which this can be done? These would be two very useful assets for my class.



I would like to be able to write something like this:



Matrix<int> A(3, 4);
std::vector<int> v {1, 2, 3, 4};
Matrix<int> row(1, 4, 3);
// *****************
A[0] = v;
A[1] = row;
// *****************


(Hopefully my variable names are self-explanatory)
I think my prototypes are correct, but I just can't find a way to do this.



Thanks!










share|improve this question























  • Why don't you use std::vector within your row class?

    – Aconcagua
    Nov 14 '18 at 3:30











  • Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

    – Aconcagua
    Nov 14 '18 at 3:32
















1















I have a slight problem. I have a Matrix class defined as follows (in row-major form):



template<typename T>
class Matrix {
private:
class RowVector {
private:
T *_vec;
std::size_t _l;

public:
RowVector(T *vec, std::size_t l);

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

operator std::vector<T>() const;
};

std::vector<T> _data;
std::size_t _m;
std::size_t _n;

public:
Matrix(std::size_t m, size_t n, const T &elem = T());

const RowVector operator(std::size_t index) const;
RowVector operator(std::size_t index);

std::size_t getm() const;
std::size_t getn() const;
void fill(const T &elem);
void fillRow(std::size_t index, const T &elem);
void fillCol(std::size_t index, const T &elem);

Matrix &transpose(unsigned int i = 1);

const std::vector<T> &data() const;
};


and wish to overload two RowVector operators=



typename Matrix<T>::RowVector &operator=(const std::vector<T> &vec);
typename Matrix<T>::RowVector &operator=(const Matrix<T> &mat);


so I can return a RowVector & using A[0] and reassign its value using either a vector or a matrix. Keep in mind that I (presumably) can ignore the rule of three because I provide no explicit way for the client to construct a RowVector object.



However, in attempting to write the function bodies for the overloads, I have come across a problem: that



(1) I cannot copy-construct a vector/Matrix object that will persist outside of the operator='s scope so that I can assign its data() to _vec and its size() to _l.



(2) I cannot directly modify _data since it is not a static variable; even if I could, I have no way to discover the index so I can overwrite the relevant region of memory in the enclosing Matrix object.



Do you know of any way in which this can be done? These would be two very useful assets for my class.



I would like to be able to write something like this:



Matrix<int> A(3, 4);
std::vector<int> v {1, 2, 3, 4};
Matrix<int> row(1, 4, 3);
// *****************
A[0] = v;
A[1] = row;
// *****************


(Hopefully my variable names are self-explanatory)
I think my prototypes are correct, but I just can't find a way to do this.



Thanks!










share|improve this question























  • Why don't you use std::vector within your row class?

    – Aconcagua
    Nov 14 '18 at 3:30











  • Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

    – Aconcagua
    Nov 14 '18 at 3:32














1












1








1








I have a slight problem. I have a Matrix class defined as follows (in row-major form):



template<typename T>
class Matrix {
private:
class RowVector {
private:
T *_vec;
std::size_t _l;

public:
RowVector(T *vec, std::size_t l);

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

operator std::vector<T>() const;
};

std::vector<T> _data;
std::size_t _m;
std::size_t _n;

public:
Matrix(std::size_t m, size_t n, const T &elem = T());

const RowVector operator(std::size_t index) const;
RowVector operator(std::size_t index);

std::size_t getm() const;
std::size_t getn() const;
void fill(const T &elem);
void fillRow(std::size_t index, const T &elem);
void fillCol(std::size_t index, const T &elem);

Matrix &transpose(unsigned int i = 1);

const std::vector<T> &data() const;
};


and wish to overload two RowVector operators=



typename Matrix<T>::RowVector &operator=(const std::vector<T> &vec);
typename Matrix<T>::RowVector &operator=(const Matrix<T> &mat);


so I can return a RowVector & using A[0] and reassign its value using either a vector or a matrix. Keep in mind that I (presumably) can ignore the rule of three because I provide no explicit way for the client to construct a RowVector object.



However, in attempting to write the function bodies for the overloads, I have come across a problem: that



(1) I cannot copy-construct a vector/Matrix object that will persist outside of the operator='s scope so that I can assign its data() to _vec and its size() to _l.



(2) I cannot directly modify _data since it is not a static variable; even if I could, I have no way to discover the index so I can overwrite the relevant region of memory in the enclosing Matrix object.



Do you know of any way in which this can be done? These would be two very useful assets for my class.



I would like to be able to write something like this:



Matrix<int> A(3, 4);
std::vector<int> v {1, 2, 3, 4};
Matrix<int> row(1, 4, 3);
// *****************
A[0] = v;
A[1] = row;
// *****************


(Hopefully my variable names are self-explanatory)
I think my prototypes are correct, but I just can't find a way to do this.



Thanks!










share|improve this question














I have a slight problem. I have a Matrix class defined as follows (in row-major form):



template<typename T>
class Matrix {
private:
class RowVector {
private:
T *_vec;
std::size_t _l;

public:
RowVector(T *vec, std::size_t l);

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

operator std::vector<T>() const;
};

std::vector<T> _data;
std::size_t _m;
std::size_t _n;

public:
Matrix(std::size_t m, size_t n, const T &elem = T());

const RowVector operator(std::size_t index) const;
RowVector operator(std::size_t index);

std::size_t getm() const;
std::size_t getn() const;
void fill(const T &elem);
void fillRow(std::size_t index, const T &elem);
void fillCol(std::size_t index, const T &elem);

Matrix &transpose(unsigned int i = 1);

const std::vector<T> &data() const;
};


and wish to overload two RowVector operators=



typename Matrix<T>::RowVector &operator=(const std::vector<T> &vec);
typename Matrix<T>::RowVector &operator=(const Matrix<T> &mat);


so I can return a RowVector & using A[0] and reassign its value using either a vector or a matrix. Keep in mind that I (presumably) can ignore the rule of three because I provide no explicit way for the client to construct a RowVector object.



However, in attempting to write the function bodies for the overloads, I have come across a problem: that



(1) I cannot copy-construct a vector/Matrix object that will persist outside of the operator='s scope so that I can assign its data() to _vec and its size() to _l.



(2) I cannot directly modify _data since it is not a static variable; even if I could, I have no way to discover the index so I can overwrite the relevant region of memory in the enclosing Matrix object.



Do you know of any way in which this can be done? These would be two very useful assets for my class.



I would like to be able to write something like this:



Matrix<int> A(3, 4);
std::vector<int> v {1, 2, 3, 4};
Matrix<int> row(1, 4, 3);
// *****************
A[0] = v;
A[1] = row;
// *****************


(Hopefully my variable names are self-explanatory)
I think my prototypes are correct, but I just can't find a way to do this.



Thanks!







c++ oop vector reference operator-overloading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 3:17









MarxMarx

61




61













  • Why don't you use std::vector within your row class?

    – Aconcagua
    Nov 14 '18 at 3:30











  • Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

    – Aconcagua
    Nov 14 '18 at 3:32



















  • Why don't you use std::vector within your row class?

    – Aconcagua
    Nov 14 '18 at 3:30











  • Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

    – Aconcagua
    Nov 14 '18 at 3:32

















Why don't you use std::vector within your row class?

– Aconcagua
Nov 14 '18 at 3:30





Why don't you use std::vector within your row class?

– Aconcagua
Nov 14 '18 at 3:30













Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

– Aconcagua
Nov 14 '18 at 3:32





Ignoring rule of three: Who has ownership for the data pointed in your row class (i. e. who is responsible for deleting it)?

– Aconcagua
Nov 14 '18 at 3:32












1 Answer
1






active

oldest

votes


















1














T *_vec;
std::size_t _l;


This is a problematic design! I don't say it is incorrect per se, but you then need to correctly manage the memory yourself. Ignoring the rule of three (five) is a very dangerous in this respect. You have a pointer to (potentially?) dynamically allocated memory, so there must be some instance that is responsible for its deletion (must not necessarily be your RowVector, but what else then?).



From pure technical aspect, you even can let _vec point to some vector's data, provided you gurantee that this other vector lives as long as you want to access the data via pointer – which in general requires, though, quite some effort.



Safest would be having each row maintain its own data, copying (or moving) it from the other vector. Then easiest is storing the data in a std::vector of its own (replacing the raw pointer).



If you want to avoid copying data around and instead share data between different matrices and their rows, then you could maintain the data via a std::shared_ptr - either maintaining a raw array or possibly even a heap-allocated std::vector.



If you opt for either std::vector or std::shared_ptr, then copy and move constructors and assignment operators get totally simple:



class C
{
public:
C(C const&) = default;
C(C&&) = default;
C& operator= (C const&) = default;
C& operator= (C&&) = default;
};


All of these defaults will do copying/moving as per member, and both std::vector and std::shared_ptr have the appropriate constructors and operators available already, so you'd be fine – and you now can violate the rule of five, dropping the destructor, as the default one (calling all the member's destructors) suffices.



If you consider shared pointers: be aware that you then cannot assign a std::vector's data to: std::vector does its own memory management, and you will end up in double deletion of, so in this specific case, you'd still have to create a copy of. You might possibly end up in multiple constructors and assignment operators:



std::shared_ptr<std::vector<int>> _data;

// assign shared pointers
RowVector(RowVector const&) = default;
RowVector(RowVector&&) = default;

// need to create copies of: we never know about the scope of the vector passed!
RowVector(std::vector<int> const& data) : _data(new std::vector<int>(data)) { }
RowVector(std::vector<int>&& data) : _data(new std::vector<int>(std::move(data))) { }

// we *are* sharing already -> no need to copy:
RowVector(std::shared_ptr<std::vector<int>& data) : _data(data) { }


Assignment operators analogously.



Side-note: If you want to have a mathmatical nxm matrix, pretty sure you don't want to have a jagged array. I'd assume your Matrix class' constructor already creates an appropriate vector of vectors, then for assignment, you'd have yet to check length:



// defaults not suitable any more!
RowVector& RowVector::operator=(RowVector const& other)
{
// still assuming shared pointer:
// (for vector, replace -> with .)
if(other._data->size() != _data.size())
{
throw SomeException();
}
_data = other._data;
}
RowVector(RowVector&& other)
{
if(other._data->size() != _data.size())
{
throw SomeException();
}
_data = std::move(other._data);
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292701%2foverriding-assignment-operators-in-proxy-class-for-vector-object-in-a-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    T *_vec;
    std::size_t _l;


    This is a problematic design! I don't say it is incorrect per se, but you then need to correctly manage the memory yourself. Ignoring the rule of three (five) is a very dangerous in this respect. You have a pointer to (potentially?) dynamically allocated memory, so there must be some instance that is responsible for its deletion (must not necessarily be your RowVector, but what else then?).



    From pure technical aspect, you even can let _vec point to some vector's data, provided you gurantee that this other vector lives as long as you want to access the data via pointer – which in general requires, though, quite some effort.



    Safest would be having each row maintain its own data, copying (or moving) it from the other vector. Then easiest is storing the data in a std::vector of its own (replacing the raw pointer).



    If you want to avoid copying data around and instead share data between different matrices and their rows, then you could maintain the data via a std::shared_ptr - either maintaining a raw array or possibly even a heap-allocated std::vector.



    If you opt for either std::vector or std::shared_ptr, then copy and move constructors and assignment operators get totally simple:



    class C
    {
    public:
    C(C const&) = default;
    C(C&&) = default;
    C& operator= (C const&) = default;
    C& operator= (C&&) = default;
    };


    All of these defaults will do copying/moving as per member, and both std::vector and std::shared_ptr have the appropriate constructors and operators available already, so you'd be fine – and you now can violate the rule of five, dropping the destructor, as the default one (calling all the member's destructors) suffices.



    If you consider shared pointers: be aware that you then cannot assign a std::vector's data to: std::vector does its own memory management, and you will end up in double deletion of, so in this specific case, you'd still have to create a copy of. You might possibly end up in multiple constructors and assignment operators:



    std::shared_ptr<std::vector<int>> _data;

    // assign shared pointers
    RowVector(RowVector const&) = default;
    RowVector(RowVector&&) = default;

    // need to create copies of: we never know about the scope of the vector passed!
    RowVector(std::vector<int> const& data) : _data(new std::vector<int>(data)) { }
    RowVector(std::vector<int>&& data) : _data(new std::vector<int>(std::move(data))) { }

    // we *are* sharing already -> no need to copy:
    RowVector(std::shared_ptr<std::vector<int>& data) : _data(data) { }


    Assignment operators analogously.



    Side-note: If you want to have a mathmatical nxm matrix, pretty sure you don't want to have a jagged array. I'd assume your Matrix class' constructor already creates an appropriate vector of vectors, then for assignment, you'd have yet to check length:



    // defaults not suitable any more!
    RowVector& RowVector::operator=(RowVector const& other)
    {
    // still assuming shared pointer:
    // (for vector, replace -> with .)
    if(other._data->size() != _data.size())
    {
    throw SomeException();
    }
    _data = other._data;
    }
    RowVector(RowVector&& other)
    {
    if(other._data->size() != _data.size())
    {
    throw SomeException();
    }
    _data = std::move(other._data);
    }





    share|improve this answer






























      1














      T *_vec;
      std::size_t _l;


      This is a problematic design! I don't say it is incorrect per se, but you then need to correctly manage the memory yourself. Ignoring the rule of three (five) is a very dangerous in this respect. You have a pointer to (potentially?) dynamically allocated memory, so there must be some instance that is responsible for its deletion (must not necessarily be your RowVector, but what else then?).



      From pure technical aspect, you even can let _vec point to some vector's data, provided you gurantee that this other vector lives as long as you want to access the data via pointer – which in general requires, though, quite some effort.



      Safest would be having each row maintain its own data, copying (or moving) it from the other vector. Then easiest is storing the data in a std::vector of its own (replacing the raw pointer).



      If you want to avoid copying data around and instead share data between different matrices and their rows, then you could maintain the data via a std::shared_ptr - either maintaining a raw array or possibly even a heap-allocated std::vector.



      If you opt for either std::vector or std::shared_ptr, then copy and move constructors and assignment operators get totally simple:



      class C
      {
      public:
      C(C const&) = default;
      C(C&&) = default;
      C& operator= (C const&) = default;
      C& operator= (C&&) = default;
      };


      All of these defaults will do copying/moving as per member, and both std::vector and std::shared_ptr have the appropriate constructors and operators available already, so you'd be fine – and you now can violate the rule of five, dropping the destructor, as the default one (calling all the member's destructors) suffices.



      If you consider shared pointers: be aware that you then cannot assign a std::vector's data to: std::vector does its own memory management, and you will end up in double deletion of, so in this specific case, you'd still have to create a copy of. You might possibly end up in multiple constructors and assignment operators:



      std::shared_ptr<std::vector<int>> _data;

      // assign shared pointers
      RowVector(RowVector const&) = default;
      RowVector(RowVector&&) = default;

      // need to create copies of: we never know about the scope of the vector passed!
      RowVector(std::vector<int> const& data) : _data(new std::vector<int>(data)) { }
      RowVector(std::vector<int>&& data) : _data(new std::vector<int>(std::move(data))) { }

      // we *are* sharing already -> no need to copy:
      RowVector(std::shared_ptr<std::vector<int>& data) : _data(data) { }


      Assignment operators analogously.



      Side-note: If you want to have a mathmatical nxm matrix, pretty sure you don't want to have a jagged array. I'd assume your Matrix class' constructor already creates an appropriate vector of vectors, then for assignment, you'd have yet to check length:



      // defaults not suitable any more!
      RowVector& RowVector::operator=(RowVector const& other)
      {
      // still assuming shared pointer:
      // (for vector, replace -> with .)
      if(other._data->size() != _data.size())
      {
      throw SomeException();
      }
      _data = other._data;
      }
      RowVector(RowVector&& other)
      {
      if(other._data->size() != _data.size())
      {
      throw SomeException();
      }
      _data = std::move(other._data);
      }





      share|improve this answer




























        1












        1








        1







        T *_vec;
        std::size_t _l;


        This is a problematic design! I don't say it is incorrect per se, but you then need to correctly manage the memory yourself. Ignoring the rule of three (five) is a very dangerous in this respect. You have a pointer to (potentially?) dynamically allocated memory, so there must be some instance that is responsible for its deletion (must not necessarily be your RowVector, but what else then?).



        From pure technical aspect, you even can let _vec point to some vector's data, provided you gurantee that this other vector lives as long as you want to access the data via pointer – which in general requires, though, quite some effort.



        Safest would be having each row maintain its own data, copying (or moving) it from the other vector. Then easiest is storing the data in a std::vector of its own (replacing the raw pointer).



        If you want to avoid copying data around and instead share data between different matrices and their rows, then you could maintain the data via a std::shared_ptr - either maintaining a raw array or possibly even a heap-allocated std::vector.



        If you opt for either std::vector or std::shared_ptr, then copy and move constructors and assignment operators get totally simple:



        class C
        {
        public:
        C(C const&) = default;
        C(C&&) = default;
        C& operator= (C const&) = default;
        C& operator= (C&&) = default;
        };


        All of these defaults will do copying/moving as per member, and both std::vector and std::shared_ptr have the appropriate constructors and operators available already, so you'd be fine – and you now can violate the rule of five, dropping the destructor, as the default one (calling all the member's destructors) suffices.



        If you consider shared pointers: be aware that you then cannot assign a std::vector's data to: std::vector does its own memory management, and you will end up in double deletion of, so in this specific case, you'd still have to create a copy of. You might possibly end up in multiple constructors and assignment operators:



        std::shared_ptr<std::vector<int>> _data;

        // assign shared pointers
        RowVector(RowVector const&) = default;
        RowVector(RowVector&&) = default;

        // need to create copies of: we never know about the scope of the vector passed!
        RowVector(std::vector<int> const& data) : _data(new std::vector<int>(data)) { }
        RowVector(std::vector<int>&& data) : _data(new std::vector<int>(std::move(data))) { }

        // we *are* sharing already -> no need to copy:
        RowVector(std::shared_ptr<std::vector<int>& data) : _data(data) { }


        Assignment operators analogously.



        Side-note: If you want to have a mathmatical nxm matrix, pretty sure you don't want to have a jagged array. I'd assume your Matrix class' constructor already creates an appropriate vector of vectors, then for assignment, you'd have yet to check length:



        // defaults not suitable any more!
        RowVector& RowVector::operator=(RowVector const& other)
        {
        // still assuming shared pointer:
        // (for vector, replace -> with .)
        if(other._data->size() != _data.size())
        {
        throw SomeException();
        }
        _data = other._data;
        }
        RowVector(RowVector&& other)
        {
        if(other._data->size() != _data.size())
        {
        throw SomeException();
        }
        _data = std::move(other._data);
        }





        share|improve this answer















        T *_vec;
        std::size_t _l;


        This is a problematic design! I don't say it is incorrect per se, but you then need to correctly manage the memory yourself. Ignoring the rule of three (five) is a very dangerous in this respect. You have a pointer to (potentially?) dynamically allocated memory, so there must be some instance that is responsible for its deletion (must not necessarily be your RowVector, but what else then?).



        From pure technical aspect, you even can let _vec point to some vector's data, provided you gurantee that this other vector lives as long as you want to access the data via pointer – which in general requires, though, quite some effort.



        Safest would be having each row maintain its own data, copying (or moving) it from the other vector. Then easiest is storing the data in a std::vector of its own (replacing the raw pointer).



        If you want to avoid copying data around and instead share data between different matrices and their rows, then you could maintain the data via a std::shared_ptr - either maintaining a raw array or possibly even a heap-allocated std::vector.



        If you opt for either std::vector or std::shared_ptr, then copy and move constructors and assignment operators get totally simple:



        class C
        {
        public:
        C(C const&) = default;
        C(C&&) = default;
        C& operator= (C const&) = default;
        C& operator= (C&&) = default;
        };


        All of these defaults will do copying/moving as per member, and both std::vector and std::shared_ptr have the appropriate constructors and operators available already, so you'd be fine – and you now can violate the rule of five, dropping the destructor, as the default one (calling all the member's destructors) suffices.



        If you consider shared pointers: be aware that you then cannot assign a std::vector's data to: std::vector does its own memory management, and you will end up in double deletion of, so in this specific case, you'd still have to create a copy of. You might possibly end up in multiple constructors and assignment operators:



        std::shared_ptr<std::vector<int>> _data;

        // assign shared pointers
        RowVector(RowVector const&) = default;
        RowVector(RowVector&&) = default;

        // need to create copies of: we never know about the scope of the vector passed!
        RowVector(std::vector<int> const& data) : _data(new std::vector<int>(data)) { }
        RowVector(std::vector<int>&& data) : _data(new std::vector<int>(std::move(data))) { }

        // we *are* sharing already -> no need to copy:
        RowVector(std::shared_ptr<std::vector<int>& data) : _data(data) { }


        Assignment operators analogously.



        Side-note: If you want to have a mathmatical nxm matrix, pretty sure you don't want to have a jagged array. I'd assume your Matrix class' constructor already creates an appropriate vector of vectors, then for assignment, you'd have yet to check length:



        // defaults not suitable any more!
        RowVector& RowVector::operator=(RowVector const& other)
        {
        // still assuming shared pointer:
        // (for vector, replace -> with .)
        if(other._data->size() != _data.size())
        {
        throw SomeException();
        }
        _data = other._data;
        }
        RowVector(RowVector&& other)
        {
        if(other._data->size() != _data.size())
        {
        throw SomeException();
        }
        _data = std::move(other._data);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 6:10

























        answered Nov 14 '18 at 5:25









        AconcaguaAconcagua

        12.2k32143




        12.2k32143






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292701%2foverriding-assignment-operators-in-proxy-class-for-vector-object-in-a-matrix%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues