Accessors for char*

Feb 15, 2012 at 9:14am
closed account (o360pfjN)
Hi all

I'm currently learning C++ from a book, however I cannot understand why the following accessors are different:

int getAge () const {return itsAge;}
const char* getName () const {return itsName;}

I can't understand why the char* would need a const as part of the return type? I appreciate that a name - such as "Bob" - all contains literal constants, but surely an actual number (eg 5) is also a constant?

Is it to ensure the pointer to the characters isn't moved? But then surely the pointer starts off by pointing to the first character in this array of letters?...

Lost! lol...

Thanks!

John

Feb 15, 2012 at 9:26am
getAge returns the value contained in itsAge. You can change the value in the variable you receive itsAge into without without changing the value contained in itsAge.

getName returns a pointer. A pointer allows you to change the contents of the original variable because it accesses memory, not just the value. So, making the pointer returned const allows you to read the value, make a copy of the value whose memory it points to, but not allow you to assign anything to it.
Last edited on Feb 15, 2012 at 9:28am
Feb 15, 2012 at 9:40am
closed account (o360pfjN)
Hi,

Thanks for the explanation! I'm still struggling to follow though :(

I understand that getAge() returns a value whilst getName() returns a pointer. But surely the value at the memory of itsAge could also be changed if the function were not constant? Also, I don't understand how the values at char* could be changed anyway, as the function is const?

I feel like either both should have const prefixes, or neither... I also don't see why const is necessary before char* if the function is already const, promising that it won't change any values.

Thanks for your help :)

John
Feb 15, 2012 at 10:26am
But surely the value at the memory of itsAge could also be changed if the function were not constant?
No. That is incorrect.

You get back a COPY of the value. This is known as pass-by-value; what gets passed in/out is not the actual object, but a COPY of the value. Contrast this with pass-by-reference (look it up when you have a moment; it's quite important).

So getAge returns a COPY of the value itsAge. This is a copy of an integer. You can do whatever you like to it, and you will not affect the actual value of itsage inside the object.

getName returns a COPY of the pointer itsName. You can do whatever you like to that copy (as the pointer isn't constant, it's a pointer to a constant), but if you dereference the copy, you'll be able to access the original stored character array inside the object. Clearly, whoever wrote this does not want you to be able to directly change those stored characters.

It is a way of ensuring that if you want to change the stored values inside the object, you have to use the functions provided and cannot just reach in there and change things yourself.

If pointers are not as easy for you as integers yet, please read this:
http://www.cplusplus.com/articles/EN3hAqkS/
Other pointer articles exist too - I just like this one :p
Last edited on Feb 15, 2012 at 10:32am
Feb 15, 2012 at 10:38am
closed account (o360pfjN)
OK... I think I'm following now. I wasn't picking up on the fact it was a copy (even though it makes sense).

To clarify, let's say I use:

cout << theDog.getAge() << endl;

this creates a copy of itsAge, to be used in cout's << operator?

So although the pointer is a copy, it is still accessing the original data (there are now two pointers to the same data). So to ensure the values being pointed to are not being changed, I need to return the data as const?

So the function:

cout << theDog.getName() << endl;

cannot include anything that will change the values being pointed to (which of course it doesn't) due to the return type?

Thanks!

John
Feb 15, 2012 at 11:05am
cannot include anything that will change the values being pointed to


That's the theory. Have a go at using the returned pointer to change the values it points to and see what happens. Hopefully, your compiler will stop you and point out that it's const.
Feb 15, 2012 at 12:09pm

So to ensure the values being pointed to are not being changed, I need to return the data as const?


Not entirely correct. The data is not constant. It can still be changed from within member functions. It's not the data that is constant, it's the pointer you receive into that is constant, so it won't allow you access to change the data.
Last edited on Feb 15, 2012 at 12:10pm
Feb 15, 2012 at 1:54pm
closed account (o360pfjN)
Thanks for the help guys,

Roberts - so other non-const pointers going to the same data could still change it, right? It's just this particular const pointer that will block editing access?

Moschops - I thought I fully understood the topic of pass by value & reference but clearly something is amiss!

I am also trying to come up with syntax that should be blocked by the complier when returning a const char*. Can you give me an example?
Feb 16, 2012 at 12:19am


I am also trying to come up with syntax that should be blocked by the complier when returning a const char*. Can you give me an example?



I'm not sure what you mean by this. My fumblings with C++ have revealed that the use of const can be as webby as polymophism.

const char * const getdata( const int &i ) const;

Topic archived. No new replies allowed.