c++ - Can I make an assignment operator on a base class that returns sub-class type -
sorry bad title... have base class like:
template<class t> class gptr { public: typedef t basetype; gptr& operator=(const basetype& rhs) { ... } };
i want make subclassed specializations like:
class graphicptr : public gptr<graphic> { ... };
however base-class assignment operator still returns gptr<graphic>
not graphicptr
, it's annoying have copy-paste code in case core assignment operator functionality should change later.
is there neat way define base-class assignment operator returns type of actual class in use?
in c++, base class has no idea of it's children. may add template parameter derived class , use it.
template<class t, class derived> class gptr { public: typedef t basetype; derived& operator=(const basetype& rhs) { ... } };