By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

Benefits of using const functions

P: n/a
What are the benefits of using const functions?
How does the compiler interpret const functions?
Thanks, Denis

Mar 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
denis wrote:
What are the benefits of using const functions?
How does the compiler interpret const functions?
Thanks, Denis From _C++ Coding Standards_ (p. 30, parentheses deleted): "const is

your friend: Immutable values are easier to understand, track, and
reason about, so prefer constants over variables wherever it is
sensible and make const your default choice when you define a value:
It's safe, it's checked at compile time, and it's integrated with C++'s
type system."

The same applies to const member functions, which essentially just make
the "this" pointer const.

See also these FAQs:

http://www.parashift.com/c++-faq-lit...rrectness.html

Cheers! --M

Mar 3 '06 #2

P: n/a

denis wrote:
What are the benefits of using const functions?
How does the compiler interpret const functions?
Thanks, Denis


Well, you can't call a non-const member on a const object. So if you
want to pass by reference in and out of functions and you want to
advertize that those functions won't change your object then having
objects without const functions become pretty much useless. This is a
major feature of C++ and I recommend becomming familiar with it. I
work on projects where people weren't familiar with const and didn't
use it while building it originally and it becomes a big mess when you
start introducing it in places it belongs.

http://www.linuxjournal.com/article/7629

Mar 3 '06 #3

P: n/a
> What are the benefits of using const functions?

You know what might get changed and what not! About the compiler, I
think const does not have any influence on the object code, in the
sense that it makes the resulting machine code slower (or faster).

Mar 3 '06 #4

P: n/a
> What are the benefits of using const functions?

You know what might get changed and what not! About the compiler, I
think const does not have any influence on the object code, in the
sense that it makes the resulting machine code slower (or faster).

Mar 3 '06 #5

P: n/a

ma*********@hotmail.com wrote:
What are the benefits of using const functions?


You know what might get changed and what not! About the compiler, I
think const does not have any influence on the object code, in the
sense that it makes the resulting machine code slower (or faster).


http://www.gotw.ca/gotw/081.htm

Mar 3 '06 #6

P: n/a
denis posted:
What are the benefits of using const functions?
How does the compiler interpret const functions?
Thanks, Denis

class Monkey
{
private:

unsigned amount_bananas;

public:

void EatBanana()
{
--amount_bananas;
}

void EatBanana() const
{
ThieveBananaFromSomwhere();
}

};
-Tomás
Mar 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.