Const

I think i got my constants straightened out. there is so many ways you can use it that it was confusing to me. the only question i have is when would you want a function to return a const. like with a regular variable or string. how do i know if i should make it constant if it isnt a reference. if the return type is like an integer and you return it as constant what happens thats different.

Last edited on
A function that returns a const non-pointer, non-reference is a function that wastes 6 keystrokes.

 
const int mul( int a, int b ) { return a * b; }


Is absolutely identical in every respect to the same function which just returns "int"

Now if you return a pointer to something or a reference to something, that's a different story.
Then it depends on whether or not you want the caller of the function to be able to modify
what is being pointed to or referred to.

Example: std::string::c_str() returns a const char*, not a char*. Why? Because std::string
does not want the user to actually modify the string it returned, because std::string owns
that memory.


thanks jsmith. it made sense to me as a reference or like you said a pointer. i just couldnt figure out why someone would do it on like an integer return and what would happen. thanks for the quick reply
Topic archived. No new replies allowed.