0

I have encountered a question which asks about Given the declaration:

void X::mf(Y &y);   

Question a: How would you change the declaration to declare that the operation of this function will not change the state of the object invoking it?

Question b: What can you do with the re-declared function that you could not do in its original form?

For Question a: I think the answer is using const keyword. something like

void X::mf(Y &y) const;

Can some one please tell me if I answered Question a) correctly. And if yes, What could be the answer for Question b).

I understood the const part but could not understand what we can do with the re-declared function that could not be done in its original form.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • 2
    Now if you have some other function that accepts a `const X& x` as a parameter, you can call `x.mf()` on it whereas without the `const` change you made for Question A, it would not compile. – AndyG May 07 '15 at 19:36
  • Your question b) is answered fairly clearly in the [accepted answer](http://stackoverflow.com/a/751783/1146608) to the duplicate question: it's exactly what AndyG points out. If you need further clarification, post a comment over there on what you don't understand. – Patrick M May 07 '15 at 21:01

0 Answers0