c++ - Template class with operator overload -
here question template class
aclass<int> a{1,2}; aclass<float> b{3.0,4.0}; aclass<int> c; int main() { c=a+b; //how overload operator in simple way? b=a; //and this? return 0; }
how can overload operator handle template class different types? (sorry poor english)
you can have member function templates inside class template:
template <typename t> struct aclass { aclass(aclass const &) = default; aclass & operator=(const aclass &) = default; template <typename u> aclass(aclass<u> const & rhs) : a_(rhs.a_), b_(rhs.b_) {} template <typename u> aclass & operator=(aclass<u> const & rhs) { a_ = rhs.a_; b_ = rhs.b_; return *this; } t a_, b_; };
Comments
Post a Comment