Sign in to follow this  

2 Overloaded Operators in 1 Statement

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
    Counter(int iniValue);
    int GetValue() const { return itsValue;}
    const Counter& operator++();
    Counter operator+(const Counter&);
    Counter operator=(Counter&);
    int itsValue;                 

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

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

const Counter& Counter::operator++()
      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;
    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;
    return 0;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this