简体   繁体   中英

operator overloading and non-member functions c++

I have written a class for complex numbers in which I have overloaded the operator + and everything works fine, however I need to implement this as a non-member function and I am not sure how, or why there is a benefit of doing so.

Here is my code .h:

class Complex
{
private:
    double a;
    double b;

public:
    Complex();
    Complex(double aGiven);
    Complex(double aGiven, double bGiven);

    double aGetValue();
    double bGetValue();    
    double operator[](bool getB);

    Complex add(Complex &secondRational);
    Complex operator+(Complex &secondRational);
}

.cpp:

Complex Complex::add(Complex &secondRational)
{
    double c = secondRational.aGetValue();
    double d = secondRational.bGetValue();
    double anew = a+c;
    double bnew = b+d;
    return Complex(anew,bnew);
}

Complex Complex::operator+(Complex &secondRational)
{
    return add(secondRational);
}

Any help on how to make these as non-member functions will be greatly appreciated!

Here is the addition operator outside of the class:

Complex operator+(const Complex& lhs, const Complex& rhs) {
  //implement the math to add the two
  return Complex(lhs.aGetValue() + rhs.aGetValue(),
                 lhs.bGetValue() + rhs.bGetValue());
}

Of course you will need to declare aGetValue() and bGetValue() as const :

double aGetValue() const {return a;}
double bGetValue() const {return b;}

You can declare a friend to your Complex class

class Complex {

// blah....

    friend Complex operator+(Complex const& a, Complex const & b);
};

The overloaded operator can access the private members of Complex.

The usual approach to arithmetic operations is to define the reflexive versions of the operators as members and the pure versions as non-members, implementing them with the reflexive versions:

class complex {
public:
    const complex& operator+=(const complex& rhs) {
        real += rhs.real;
        imag += rhs.imag;
        return *this;
    }
};

complex operator+(const complex& lhs, const complex& rhs) {
    complex res(lhs);
    res += rhs;
    return res;
}

How is explained above by pippin1289.

Why is explained below:

Imagine one need to use object of class as

Complex c3 = 5 + c1;// for c3 object c1's real part (a) added with 5

As C++ preserve order of operand. Compiler resolve above addition call as 5.operator+ (const Complex & other);// which is not possible Hence, overload it via free function.

Your class is exposing necessary information via public interface such as aGetValue() and bGetValue. Hence, this free overloaded + operator function need not be friend of class.

Additionally, Prefer non friend non member function over member function as it helps reduce degree of encapsulation. This is explained here ==> http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197?pgno=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM