reset password
Author Message
busynerd
Posts: 5
Posted 21:03 Jan 27, 2014 |

I can't seem to run this line of code without error: 

(w2+w3).output();

unless I remove the const from the overloaded operator like so:

before: const Weight operator +(const Weight& w) const;

after: Weight operator +(const Weight& w) const;

----------------

If I leave it this way: const Weight operator +(const Weight& w) const;

I have to create a new object w4, do the operation w2+w3 and assign it to w4, and then I can print it out, w4.output();

----------------

I get that the const is preventing me from changing data in certain objects, but what else is going on and what's best practice?

thanks!

kknaur
Posts: 540
Posted 07:05 Jan 28, 2014 |

You can email me your code and I will take a look at it.  You should keep the method signatures exactly as they are given in the assignment prompt, otherwise points will be deducted. Now for the explanation, please read carefully:

 

Let's look a two different ways to define a function in relation to classes and then see how placing const in different places affects those functions. First I will give an example of a nonmember function (that is a function which is NOT part of a class implementation).

 

const Weight operator +(const Weight& w1, const Weight& w2);

 

const: When you place const in the red position you are specifying that the object the function is returning should be constant. Meaning, that the anonymous object that gets returned should not be able to be directly altered. This is normally the behavior we want, because when we overload an operator we normally want to overload the operator in such a way that it retains some of the meaning of its non-overloaded form. In other words, you normally wouldn't want to do something like this:

 

(w1 + w2).someFunction()

 

because if you use the + operator in its normal way you wouldn't (and couldn't) do something like:

 

(2 + 2).someFunction()

 

So we make the returned anonymous object constant so that way we can preserve some of the original way that the plus operator is used. It is more intuitive to assign the anonymous object that gets returned to another variable of the same type and THEN make function calls using the new variabe such as in:

 

Weight w3 = w1 + w2;

w3.someFunction();

 

Now remember with this example, when you say w3 = w1 + w2 you are copying the member variables of the anonymous object into the member variables of the w3 object. When you do this w3 is a brand new object which is NOT constant so you can alter it any way you want.

 

const: When you place const in the blue position you are simply saying that the first object passed into the function is not allowed to be altered by that function.

 

const: When you place const in the green position you are simply saying that the second object passed into the function is not allowed to be altered by that function.

 

Now lets look at an example of overloading + as a member function (that is as a function defined inside of the class)

 

const Weight operator +(const Weight& w) const;

 

const: When you place const in the red position in this example, you are still specifying that the object the function is returning should be constant. The rest of the explanation is the same as above.

 

const: When you place const in the blue position you are simply saying that the calling object (which is implicitly passed into the function) is not allowed to be altered by that function.

 

const: When you place const in the green position you are simply saying that the parameter object (which is explicitly passed into the function) is not allowed to be altered by that function.

 

Again as I said in class, most c++ programmers seem to have their on opinion about whether or not to return a const value.  As far as this class is concerned, we will follow the textbook's recommendations in order to maintain some consistency.  Once you are out of the class and you wish to develop your own c++ style, and you can fully justify NOT returning a const value then by all means please feel free to do so.  Again, for the duration of the class we will be following what the book does.

Last edited by kknaur at 09:37 Jan 28, 2014.
kknaur
Posts: 540
Posted 12:49 Jan 28, 2014 |

Let me also say that (w1 + w2).output() would be acceptable to me as long as output() does not alter the anonymous object in any way.  If you want to tell the compiler that output() does not alter the calling object, you need to place const at the end of the function header.