const keyword

Started by
6 comments, last by iMalc 18 years, 4 months ago
Hi all, Ok it seems to be trival question but what is the difference (there must be sth cause it works in one case and not in the other) between:

int function( void ) const;

and

const int function( void );
Thx in advance
Advertisement
Quote:Original post by enmaniac
Hi all,

Ok it seems to be trival question but what is the difference (there must be sth cause it works in one case and not in the other) between:

int function( void ) const;andconst int function( void );


Thx in advance


The former usage declares the method as being const--which means it cannot modify non-mutable (IIRC) members.

The latter usage denotes the returned int as being const.
You'll most often see it like this:

const int SomeClass::getAnInt(){// This function returns an int that can't be changed....}


OR

bool SomeClass::operator ==(const SomeClass& rhs) const{// This function is not allowed to change anything in this object// OR in the argument object....}


Const correctness - ensuring that functions that shouldn't change anything (like the == operator) can't, or that variable you will later rely on can't be altered is good practice. Think of it like saying 'This shouldn't change anything, bitch at me if I try' to the compiler.
Winterdyne Solutions Ltd is recruiting - this thread for details!
http://www.parashift.com/c++-faq-lite/const-correctness.html
Thx all of you! It is clear now :-) Stupid me :-)
Since I don't feel my question warrants a whole new thread, and it's on the topic of constness, here it is.. . Sorry for semi-hijacking your thread OP [grin]

What's the general opinion on returning by value with regards to const correctness? When you return by value, do you make it const, or not? Since it's by value, there's no obvious gain in 'security', and presumably none in speed either, unless your function is guarenteed to return the same thing every time, which is notmally not the case. But perhaps I'm missing something? Anyway, so far, my returns by value have been non-const, but I'm wondering what you all use?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Depends! If you want to make sure a client (calling) function uses only the return value it is supplied, make it const -

For example, getting the screen position of the cursor should return a const point&, not just a point or point&, cos the client could change those. Also, for larger structures a const reference is smaller to pass on the stack than the structure itself, and causes less allocator thrashing.

Returning a const just makes it harder for the caller to abuse the information you provide it.

Winterdyne Solutions Ltd is recruiting - this thread for details!
If you don't declare the result of a + operator as const (for example), then the compiler will quite happily ignore the mistake:
if (a + b = c)
where you meant to write
if (a + b == c)
This is because a + b returns a non-const temporary object which can then be assigned the value of c.

If you make it return a const object then a + b is const temporary and cannot be assigned the value of c, thus the compiler can spot the error for you.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement