Declaring functions outside a structure

Hello, how can I declare the following outside the structure?

#define do_op(o) \
inline void operator o##= (const XYZ& b) { for(unsigned n=0; n<3; ++n) d[n] o##= b.d[n]; } \
inline void operator o##= (double b) { for(unsigned n=0; n<3; ++n) d[n] o##= b; } \
XYZ operator o (const XYZ& b) const { XYZ tmp(*this); tmp o##= b; return tmp; } \
XYZ operator o (double b) const { XYZ tmp(*this); tmp o##= b; return tmp; }
do_op(*)
do_op(+)
do_op(-)
#undef do_op
1
2
3
4
5
6
7
8
9
#define do_op(o) \
inline void operator o##= (XYZ &a, const XYZ& b) { for(unsigned n=0; n<3; ++n) a.d[n] o##= b.d[n]; } \
inline void operator o##= (XYZ &a, double b) { for(unsigned n=0; n<3; ++n) a.d[n] o##= b; } \
XYZ operator o (const XYZ &a, const XYZ& b) { XYZ tmp(a); tmp o##= b; return tmp; } \
XYZ operator o (const XYZ &a, double b) { XYZ tmp(a); tmp o##= b; return tmp; }
do_op(*)
do_op(+)
do_op(-)
#undef do_op 
You'll need to declare the operators as friends for this to work.

Also, assignment and compound assignment operators should always return a reference to the left-hand object or *this.

By the way, functions defined inside a class are always implicitly inline.
Last edited on
Topic archived. No new replies allowed.