Operator commonalities

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
 
R

Rolf Magnus

Martin said:
Hi!

I have a class C with one type template parameter T and a member of
type T.

You mean you have a class template called C.
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?

Why would you want to?
 
M

Martin Eisenberg

Rolf said:
You mean you have a class template called C.


Why would you want to?

Come to think of it, why would anyone actually want to avoid
duplication? Seriously, I don't get your question.
 
V

Victor Bazarov

Martin Eisenberg said:
Come to think of it, why would anyone actually want to avoid
duplication? Seriously, I don't get your question.

template<class T> class C {
T t;
void commonCode() { /*whatever*/ }
public:
C(T t) : t(t) {}
C& operator =(C const& otherC) {
commonCode();
t = otherC.t;
return *this;
}

template<class U> C& operator =(C<U> const& otherCU) {
commonCode();
return *this;
}
};

int main() {
C<int> ci(42);
C<double> cd(3.1415926);
ci = cd;
}

Perhaps I don't understand your problem. Do you think you could
illustrate with C++?

Victor
 
D

David Fisher

Martin Eisenberg said:
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?
template<class T>
class C {
public:
template said:
C<T>& operator=(const C<T>& rhs)
{ t_ = rhs.t_; data_ = rhs.data_; return *this; }

template<typename RhsT> // <-------- added "typename"
C<T>& operator=(const C<RhsT>& rhs)
{ data_ = rhs.data_; return *this; }

private:
int data_;
T t_;
};

(Needed to add "template <>" to the first declaration, at least under Visual
C++).

This won't quite work anyway ... "data_ = rhs.data_" is illegal, since in
the context of C<T>, C<RhsT>::_data is private

David F
 
M

Martin Eisenberg

Victor said:
Perhaps I don't understand your problem. Do you think you could
illustrate with C++?

Duh... You've understood my "problem" perfectly well. Sometimes I
think terminal blindness is but weeks away. Thanks anyway,


Martin
 

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,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top