NRVec wrapper class: issue with the implementation of the assignment operator


garyzhang
06-20-2006, 09:40 PM
Dear all,

I think, in the NRVec wrapper class, the implementation of the assignment operator "inline NRVec& operator=(const T &rhs) {myvec = rhs; return *this;}" does not always yield expected result. (see pg 29)

Here is the situation in which that implementation seems to be problematic.

MyVec<int> my(3);
NRVec<int> nr(my); // using the conversion constructor
nr = 1;

Since the conversion constructor is implemented by assigning the reference of the argument (my) to the member variable (MyVec<T> &myref), the other member variable (MyVec<T> myvec) becomes inaccessible to the outside world because other functions in NRVec access the object only through (MyVec<T> &myref).

In this case, the assignment operator in (nr = 1) assigns the new value to the inaccessible variable (MyVec<T> myvec). Consequently, (MyVec<T> &myref) is not changed. In effect, the assignment fails to work.

Anybody agree?

Gary