Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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(); // fine
p.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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this