Skip to main content
gamedev.net
🔒 Locked

const char * functionName() const ????

Started by GekkoCube May 4, 2004 at 10:38 PM 2 replies 300+ views
Original Post
GekkoCube
GekkoCube
I need a refresher on this. What exactly does this mean again? the const char* mean the pointer returned by the function is constant, so the pointer will always point to an address. 1) But what does the const mean after the function name? 2) And since the returned pointer is a const char*, how can i manipulate the data the pointer is pointing to (like an array of char''s)?
Miserable
Miserable
quote:
Original post by GekkoCube
I need a refresher on this.

What exactly does this mean again?

the const char* mean the pointer returned by the function is constant, so the pointer will always point to an address.

No, it means that the pointer, which is non-constant and can be assigned to, points to constant data.

quote:
1) But what does the const mean after the function name?

For a member function, const after the function header means that the function will not modify the object.

quote:
2) And since the returned pointer is a const char*, how can i manipulate the data the pointer is pointing to (like an array of char's)?

You can't; see above. Note: A constant pointer to non-constant characters is a char* const; a constant pointer to constant characters is a const char* const.


[edited by - Miserable on May 4, 2004 11:47:37 PM]
GekkoCube
GekkoCube
ok first of all thanks.

this is surprising to me because i have broken those rules without the compiler complaining about it.
i had the following:

/// in header file.
const unsigned char * getData() const;

And this is how i used this function:


unsigned char* pData = (unsigned char*)getData();

in size = 8;

for (int i=0; i<size; i++)
{
pData += 50;
}


1) it suprised me that i cant manipulate the data by adding 50 to each element.

2) and i was surprised that i could typecast the returned type to (unsigned char*) which effectively made the const unsigned char* no longer constant!
davepermen
davepermen
you use a c-style cast. they can do anything (nearly).

in the end, it will map to a const_cast => yes, you casted away the constness.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.