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 :)
Nusrat NuriyevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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

0
jkrCommented:
OK, and now the working example:

#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;}
  
    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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nusrat NuriyevAuthor Commented:
>> 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?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

jkrCommented:
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)
0
jkrCommented:
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...
0
Nusrat NuriyevAuthor Commented:
Yeah, ok :)
Searching for proper concentration? :)
product-management-101-1-how-to-create-p
0
jkrCommented:
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.
0
Nusrat NuriyevAuthor Commented:
Seriously, while I hardly would decline a drink in my spare time, I for sure will do that while working.
:)))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.