c++ - Overloading operator+ with pointers -
i'm working on project on polymorphism in c++ , have lots of pointers. need overload operator +, can write following expression naturally:
c=a+b;
with a,b , c being declared as:
a *a,*b,*c;
the basic overloading definitions know are
a &operator+(const a& a) &operator=(const a& a)
but following errors:
invalid initialization of non-const reference of type 'a&' rvalue of type 'a* const' invalid operands of types 'a*' , 'a*' binary 'operator+'
how should write overloaded operator can call naturally?
i know following suggestion not want, sake of completeness tried find solution template wrapper class around pointers would permit overloading operator+. wondered combination of lookup rules, template arguments , type conversion achieve. turns out "natural" use of operator+
seems impossible, explicit call works. here code:
/** wrapper class around arbitrary pointer types */ template<class tptr> class myptrt { public: tptr ptr; myptrt(const tptr p = 0) :ptr(p) {} }; /** plus operator on myptrts */ template<class t> const myptrt<t>& operator+(const myptrt<t> &l, const myptrt<t> &r) { return l; } myptrt<int *> mp1, mp2, mp3; int *ip;
ok, works these declarations?
// works (template type explicit) ... mp3 = operator+<int *>(ip, ip); // ... , works (of course, // arguments match template declaration) ... mp3 = mp1 + mp2;
and doesn't (with gcc 4.8.3)?
mp3 = mp1 + ip;
the error is:
smartpointplus.cpp:33:12: error: no match ‘operator+’ (operand types ‘myptrt<int*>’ , ‘int*’) mp3 = mp1 + ip; ^ smartpointplus.cpp:33:12: note: candidate is: smartpointplus.cpp:13:1: note: template<class t> const myptrt<t>& operator+(const myptrt<t>&, const myptrt<t>&) operator+(const myptrt<t> &l, const myptrt<t> &r) ^ smartpointplus.cpp:13:1: note: template argument deduction/substitution failed: smartpointplus.cpp:33:14: note: mismatched types ‘const myptrt<t>’ , ‘int*’
i'm not quite sure why compiler doesn't construct myptrt int* argument. see proper function. perhaps wrong ctor?
mp3 = operator+(ip, ip);
the error message similar. template function considered compiler cannot convert arguments.
smartpointplus.cpp:36:24: error: no matching function call ‘operator+(int*&, int*&)’ mp3 = operator+(ip, ip); ^ smartpointplus.cpp:36:24: note: candidate is: smartpointplus.cpp:13:1: note: template<class t> const myptrt<t>& operator+(const myptrt<t>&, const myptrt<t>&) operator+(const myptrt<t> &l, const myptrt<t> &r) ^ smartpointplus.cpp:13:1: note: template argument deduction/substitution failed: smartpointplus.cpp:36:24: note: mismatched types ‘const myptrt<t>’ , ‘int*’ mp3 = operator+(ip, ip);
it's bit sad operator+(ip, ip)
(automatically trying deduce template type) doesn't work. limits usability ;-). 1 have templatize functions using operator in order avoid providing template type explicitly every call. infix notation below, contrast, doesn't consider template operator:
mp3 = ip + ip;
the error message short:
smartpointplus.cpp:38:13: error: invalid operands of types ‘int*’ , ‘int*’ binary ‘operator+’ mp3 = ip + ip;
i thought infix , function style notation of operator calls just, well, notational differences, apparently change lookup rules.
tl;dr: op wanted (an infix plus operation pointer arguments) doesn't work template wrapping class, afaict.
Comments
Post a Comment