Jump to content
  • Advertisement
Sign in to follow this  
Rad94

2 Overloaded Operators in 1 Statement

This topic is 3892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm learning C++ from Sam's Teach Yourself C++ in 21 days and I was wondering how you use 2 overloaded operators in 1 statement. My program:
#include <iostream>

using namespace std;

class Counter
{
    public:
    Counter();
    Counter(int iniValue);
    int GetValue() const { return itsValue;}
    const Counter& operator++();
    Counter operator+(const Counter&);
    Counter operator=(Counter&);
    
    private:
    int itsValue;                 
};

Counter::Counter(): itsValue(0) {}

Counter::Counter(int iniValue): itsValue(iniValue) {}

const Counter& Counter::operator++()
{
      ++itsValue;
      return *this;
}

Counter Counter::operator+ (const Counter& Counter2)
{
   return Counter(itsValue + Counter2.GetValue());                 
}

Counter Counter::operator=(Counter& rhs)
{
    if (this == &rhs)
    return *this;
    
    itsValue = rhs.GetValue();
    return *this;     
}                                   

int main(int argc, char *argv[])
{
    Counter aCounter(1), aCounter2(2), aCounter3;
    ++aCounter;
    ++aCounter2;
    aCounter3 = aCounter;
    cout << "Value is " << aCounter3.GetValue() << endl;
    aCounter3 = aCounter + aCounter2; //Error here.  I can't use 2 overloaded operators in 1 statement.  
    cout << "Value is " << aCounter3.GetValue() << endl;
    system("PAUSE");
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!