reference in operator=

D

devphylosoff

hi

i have class array, and operator= :
I remove

.....
template <typename T2> array<T> operator= (const array<T2> rhs) {
if (this != &rhs) {
resize(rhs.size());

// a now I have 3 others ways :
/* 1 attempt */ for (iterator i=begin(); i!=end(); i++)
*i = rhs;

/* 2 attempt */ std::copy(rhs.begin(),rhs.end(), begin());

/* 3 attempt */ for (int i = 0; i < max_size(); i++)
elems = *rhs;
} return this;
}


i want copy one array to another
in loop: assert(*it1 == *it2);
then increment all elements in second array, so below assertion should
be correct:
in loop: assert(*it1 != *it2);
but is not - whatever i do on only one array in fact i do on both
-there are the same array - no matter which the above attempt I use.
I change:
array<T>& operator= (const array<T2>& rhs)
to
array<T> operator= (const array<T2> rhs)

without success.

how to do copy, not reference ?
 
L

lkpo1988

i think this will ok

template <typename T2>
array<T2>& operator= (const array<T2> rhs) {
if (this != &rhs)
{
resize(rhs.size());
for (int i = 0; i < size(); ++i)
this-> = rhs;
}
return *this;
}

it's what you need?
 
D

devphylosoff

i think this will ok

i also think that, but it doesn't ok
strange.

here I have files
http://student.if.uj.edu.pl/Slawomir.Lenart/cpp/
boost_array_NEW.hpp - lines 88 - 103 - operator=
boost_array.cpp - test for my class
rest of files boost concept check or additional tools.

should I move operator= out of class, and define only
template <typename T> array<T> operator= (const array<T> rhs) ...
not array<T2> - but i dont know said:
template <typename T2>
array<T2>& operator= (const array<T2> rhs) {
if (this != &rhs)
{
resize(rhs.size());
for (int i = 0; i < size(); ++i)
this-> = rhs;
}
return *this;

}

it's what you need?


no
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top