C++ Pre-fix and Post-fix operator overloading for addition (+) (not ++ or --) -


this question has answer here:

i writing complex class assignment in 1 of methods should overload default addition operator when adding double complex number. far, have following code correctly works c+5 c complex object

complex& complex::operator+(const double& d) const {     return complex(real + d, imag); } 

however, doesn't when 5+c. think might because of prefix post-fix thing not sure.

my question if there way overload + operator can 5+c. tried searching solution online answers find dealt increment/decrement operators add int argument post-fix. tried same thing + doesn't work.

thank much.

two options come mind:

1) implement 2 non member functions:

complex operator+(const complex& lhs, double rhs); complex operator+(double lhs, const complex& rhs); 

2) make complex implicitly constructable double (if isn't already), , implement single non-menber:

complex operator+(const complex& lhs, const complex& rhs); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -