Read only function

Hi there I have a question regarding const but after looking online I still cannot understand what it means exactly.

My question is as follows:

1
2
3
4
5
6
7
The class Shoe has an int attribute called size and a getSize() method which returns an int. A programmer has written two versions of the getSize() function, shown here as (i) and (ii):
(i) int getSize() const {return size;}
(ii) int getSize() {return size;}

(a) Explain the difference between (i) and (ii).
(b) Give an example of code which would compile with version (i) but not with version (ii) and explain why this is so.
(c) Write the code that creates a constant pointer to the Shoe object aTrainer.



Could you please explain what the answer to question (a) is and provide some example code. The online references for read only members (which I believe this is??) are extremely vague and give no indication as to if I am correct or not.


My answer to the question is as follows (but I do not know if it is correct):

 
i) Is a read only function that cannot be modified within the function itself meaning that you could not edit any value in the function but are only able to read and return value.



Although I had a second answer to a similar question which is:

 
Function i) has a const before the left curly bracket meaning that the function can operate on a const variable. This means that if size was declared a const, then only i) would work.



Please advise me on the correct solution, thank you for any help you can provide

Chazzmundo
Last edited on
Your answer is almost correct. It's not the function that cannot be modified though..

Normally this is passed as Class* const (a constant pointer to some object), however to a const method this is passed as const Class*const (a constant pointer to a constant object) meaning that no members of this can be modified from that function.
I still do not fully understand?

What would be legal and illegal code for an answer to b and why?
You can convert pointers from Class* to const Class*, but not the other way around. Thus if you have a constant object (or more commonly, a const Class&), you won't be able to pass it as a Class*. Code that tries to call non const methods with a const object will fail.
So what you are saying is that my second answer is close but the first answer I supplied is off?
No, the first one isn't perfect, but the second one is totally wrong.
A const modifier on a method prevents you from modifying any member variables from within that method. In other words, if you declare a method as const, you can pretend that every single member variable belonging to that class is declared const, effectively making the internals of the class read-only for that particular method.

Note that you can still create and modify local variables within const methods (see GetDiameter below), the only restriction is that you cannot modify member variables (such as m_radius, below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Circle {
public:
  void SetRadius(int radius) { // no const modifier on method, so it is ok to modify m_radius here
    m_radius = radius;
  }

  int GetRadius() const { // const modifier effectively makes m_radius (and any other member variables const)
    // m_radius = 10; // This would cause a compiler error because of the method's const modifier
    return m_radius;
  }

  int GetDiameter() const {
    int diameter = GetRadius() * 2; // It is ok to create local variables
    return diameter;
  }

private:
  int m_radius;
};


Edit: In order to answer part two of your question, you should also know the following.

If a method has a const modifier, it cannot call any methods which do *not* have const modifiers. In other words, you would not be able to cheat the const system by calling SetRadius() from within GetRadius in order to modify m_radius indirectly.
Last edited on
Thank you that was extremely helpful. I understand the concept now but the only thing I am having a little issue with is how you could answer question (b) with this


 
(b) Give an example of code which would compile with version (i) but not with version (ii) and explain why this is so


I can not think of a situation where code would work with (i) but not (ii) because (i) is the one with restrictions placed upon it.

For example:

 
int getSize() {size+=1; return size;}


would not compile with (i) but it would with (ii) which is the opposite of what the question is asking?

Could someone please supply some code that will compile with (i) and not with (ii) please as this is a question that will most certainly pop up in my exam tomorrow and I can't think of any code that would answer question (b) given the explanation above about the differences.

Thank you for any help.
I wrote:
You can convert pointers from Class* to const Class*, but not the other way around. Thus if you have a constant object (or more commonly, a const Class&), you won't be able to pass it as a Class*. Code that tries to call non const methods with a const object will fail.
Edit
Report
jeffbmartinez wrote:
If a method has a const modifier, it cannot call any methods which do *not* have const modifiers. In other words, you would not be able to cheat the const system by calling SetRadius() from within GetRadius in order to modify m_radius indirectly.
Even with these hints I do not understand the atcual code that would be needed. I do not see how any code created would work for (i) but not for (ii) even though (i) is the exact same as (ii) but with extra restrictions.

So by that logic surely anything that works for (i) would work for (ii) which is the complete opposite of what the question is asking??
Do you realize that there might exist objects that cannot be modified? Now, a function without const might modify something. So how can you modify something you can't modify?

1
2
3
4
5
6
struct A{ void func(){/*might modify something*/}; }
int main(){
   const A a;
   a.func();
   return 0;
}
Hopefully not too late for your exam, but play around with my Circle example above and you'll find it. Also, maybe you missed it because I added it later, but see the edit I added for an explanation of what you're looking for.

Major hint: I wrote that code that way on purpose so all you need to do is remove one of the const modifiers to see an example of (i) but not (ii).

There's only two consts in there, so you have a 50/50 chance of getting it right the first time even if you give it zero thought! :)
Topic archived. No new replies allowed.