overloading operators

const MyClass MyClass::operator+(const MyClass &other) const
{.....................}

Please can someone explain to me in very simple words...why the last term "const" is outside the bracket and standing alone...i mean it doesn't make sense to me but i know it means something... thanks...

http://www.youtube.com/watch?v=ehJKvbbe9hk

The last const means that you cannot modify the "this" object

1
2
3
4
5
6
7
8
9
10
11
12
13
class MyClass
{
      int x;
   public:
      MyClass(){x=0;}
      const operator+(const MyClass &other) const;
}

const MyClass MyClass::operator+(const MyClass &other) const
{
    x+=other.x;    //Illegal
    return *this;
}


The above code is illegal since it will modify x
hahahah thank you...i like how you said...the code is "illegal" lol...you mean one can be arrested for using it? hahahaha :D thanks anyway
Yes, the compiler will arrest you and place you in jail for making a mistake - your punishment is that it refuses to make the program for you.
I understood that LB ;)
Topic archived. No new replies allowed.