Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

Internal overloading vs. external overloading of binary and unary operators.

Hello,

In this tutorial the operator overloading is explained via friend function.
http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/
When the operator does not modify its operands, the best way to overload the operator is via friend function.

Does there exist overloading operator(for example, binary plus, unary plus) via internal overloading?
If so, provide working example to binary, unary pluses with external and internal overloading with your comments.

Thank you :)
Avatar of jkr
jkr
Flag of Germany image

This can work if the types involved are unrelated to the ordering, i..e. it does not matter on which side of the operator they are placed. To stay within that 'Cent' example, consider

#include <iostream>

class Cents
{
private:
    int m_nCents;
 
public:
    Cents() { m_nCents = 0;}

    Cents(int nCents) { m_nCents = nCents; }

    Cents operator+(int nCents) { m_nCents += nCents; return *this;}
 
    Cents operator+(const Cents &cCents) { m_nCents += cCents.GetCents(); return *this;}
  
    const int GetCents() const { return m_nCents; }
};

int main()
{
    Cents c1 = Cents(4) + 6; // OK, covered by 'operator+(int nCents)'
    Cents c2 = Cents(6) + Cents(4); // OK, covered by 'operator+(const Cents &cCents)'
    Cents c3 = 6 + Cents(4); // BOOM, 'operator+(const Cents &cCents)' only works 'right handed' 
    std::cout << "I have " << c1.GetCents() << " cents." << std::endl;
    std::cout << "I have " << c2.GetCents() << " cents." << std::endl;
 
    return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nusrat Nuriyev

ASKER

>> OK, and now the working example
:D
Seems to me you have compiled this example, great answer, right + left handed coverage was really great contribution.

just tell me about unary... I thing it was little bit weird if unary plus overloading is ever existed, even unary minus is under doubt, I think overloading them just non-sense and not possible? Am I right?
Mind if I let you hang in until tomorrow evening your time? Had a little too much Vodka to cover that now without having to correct myself later ;o)
You can overload all unary operators include '-' and '!', as well as both post- and pre-increment operators, e.g.

#include <iostream>

class Cents
{

    friend Cents operator+(int,const Cents&);
private:
    int m_nCents;
 
public:
    Cents() { m_nCents = 0;}

    Cents(int nCents) { m_nCents = nCents; }

    Cents operator+(int nCents) { m_nCents += nCents; return *this;}
 
    Cents operator+(const Cents &cCents) { m_nCents += cCents.GetCents(); return *this;}

    Cents& operator++() { ++m_nCents; return *this;};       // Prefix increment operator.
    Cents operator++(int) { Cents tmp = *this; ++*this; return tmp;};     // Postfix increment operator.
  
    Cents operator-() {m_nCents = -m_nCents; return Cents(m_nCents;} // even though I doubt this makes any sense :-D

    const int GetCents() const { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(int nCents, const Cents &cCents)
{
    return Cents(cCents.m_nCents + nCents);
}

int main()
{
    Cents c1 = Cents(4) + 6; // OK, covered by 'operator+(int nCents)'
    Cents c2 = Cents(6) + Cents(4); // OK, covered by 'operator+(const Cents &cCents)'
    Cents c3 = 6 + Cents(4); // OK, covered by the global 'operator+(int, const Cents&)'
    std::cout << "I have " << c1.GetCents() << " cents." << std::endl;
    std::cout << "I have " << c2.GetCents() << " cents." << std::endl;
    std::cout << "I have " << c3.GetCents() << " cents." << std::endl;
 
    return 0;
}
                                          

Open in new window


The site you linked has something about that, too: http://www.learncpp.com/cpp-tutorial/95-overloading-unary-operators/ - yet they seem to love their global friends...
Yeah, ok :)
Searching for proper concentration? :)
product-management-101-1-how-to-create-p
LOL, no, found mine already - it's zero ;o)

Seriously, while I hardly would decline a drink in my spare time, I for sure will do that while working.
Seriously, while I hardly would decline a drink in my spare time, I for sure will do that while working.
:)))