# const in function (C++)

This topic is 3196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well since I'm not really busy these days I've decided to pick up learning C++ again.... Something that made wonder is the use of const: when to use and what does it actually do.. Ie in the following function:
const RETURNTYPE FUNC(const ARGTYPE ARGNAME) const {FUNCTIONBODY};
I know the "second" says that the passed argument can't be changed.. However what do the first and last actually "do"? - And when should I use them? (I guess the first const makes the returned value a constant: so that you can't actually change it anymore, and can only pass it to function where the argument is declared with "const": but why would anyone do that, isn't it a bit limiting?) - And what does the third "const" mean?

##### Share on other sites
Before asking for further correction on const, read the C++ FAQ entry on const. Let us know what questions you have after it.

##### Share on other sites
First one goes with the return type
Last one is only for non-static member functions and means you can't modify members of that instance of a class (and also can't call non-const member functions from that one). The only exception being mutable members. Trying to have a style where it becomes obvious:
string const & myclass::get_name( void ) const{    return this->name;}

This returns 'name' but because of the string const &, you can't change it. Note that const string & and string const & are the same, although you'd probably rather try to use the version I used above. Also, the const after the method name means the method won't modify the instance, which is the case here. For example, this wouldn't work because of the const:
void myclass::set_name( string const & n ) const{    this->name = n;}

However, this would work (it's a very bad example as far as the design is concerned, but it's just so you see the syntax):
struct myclass{    void set_name( string const & n ) const    {        this->name = n;    }    mutable string name;};

Note that if you had a const instance of myclass, you could only call methods that have const at the end of their declaration, since you're not supposed to be able to modify the instance:
struct myclass{    string const & get_name( void ) const { return this->name; }    void set_name( string const & n ) { this->name = n; }};myclass const p;p.get_name(); // finep.set_name( "hello" ); // won't compile, p is const and set_name isn't

Hope it's clear, it's kinda difficult to explain because after using it for centuries it just becomes automatic and you don't really think about it heheh...

##### Share on other sites
Thanks for the faq/explanation... I think I get the point now.
Yeah, I noticed it's some kind of automatism for most programmers: but that's even more confusing when you read tutorials/books - those tend to go quickly over it (if they do it at all) and then always use it.

Just 1 small question:
const void FUNC....

I don't think it would: but does that actually have ANY effect?

Also so if I'm right: if I use "const" after the function it only has any effect if it is a member function of some kind? (and else it's just 5 extra useless letters typed)

##### Share on other sites
Putting const at the end of a non-member function should fail to compile. Qualifying void with const doesn't have any effect that anyone posting in For Beginners should care about.

##### Share on other sites

This topic is 3196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628722
• Total Posts
2984396

• 25
• 11
• 10
• 16
• 14