M
Martin Eisenberg
Hi!
I have a class C with one type template parameter T and a member of
type T. Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively. The
implementations of these differ only in that the latter does not
assign the T member. How do I best get to write the common part only
once?
Here's the situation for those who read code better than English. Of
course, my real class and operators are bigger than that:
template<class T>
class C {
public:
C<T>& operator=(const C<T>& rhs)
{ t_ = rhs.t_; data_ = rhs.data_; return *this; }
template<RhsT>
C<T>& operator=(const C<RhsT>& rhs)
{ data_ = rhs.data_; return *this; }
private:
int data_;
T t_;
};
Thank you,
Martin
I have a class C with one type template parameter T and a member of
type T. Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively. The
implementations of these differ only in that the latter does not
assign the T member. How do I best get to write the common part only
once?
Here's the situation for those who read code better than English. Of
course, my real class and operators are bigger than that:
template<class T>
class C {
public:
C<T>& operator=(const C<T>& rhs)
{ t_ = rhs.t_; data_ = rhs.data_; return *this; }
template<RhsT>
C<T>& operator=(const C<RhsT>& rhs)
{ data_ = rhs.data_; return *this; }
private:
int data_;
T t_;
};
Thank you,
Martin