28

Possible Duplicate:
What is the meaning of a const at end of a member function?

If my class definition is as follows:

type CLASS::FUNCTION(int, const char*) const

What does the last const after the closing bracket mean, and how do I apply it to the function:

type CLASS::FUNCTION(int var1, const char* var2) {

}
Community
  • 1
  • 1
Matt Dunbar
  • 950
  • 3
  • 11
  • 21
  • 2
    Bound to be a dupe, but easier to answer than look for it. It means that the member function can be called on a `const` object (or via a reference-or-pointer-to-const), and that the member function cannot modify data members (unless they're marked `mutable`). In effect it makes `this` a pointer-to-const in the function. Usually you say "FUNCTION is a const member function", can't remember if that terminology is in the standard though. – Steve Jessop Mar 04 '11 at 23:58
  • @Steve: That's been answered in an __[FAQ entry](http://stackoverflow.com/questions/4059932/what-is-the-meaning-of-a-const-at-end-of-a-member-function)__. I found that by typing `c++-faq` into my browser's address bar, which took me to [http://stackoverflow.com/questions/tagged/c++-faq](http://stackoverflow.com/questions/tagged/c%2b%2b-faq), where I searched for `const `, which gave me the URL. Less than 30secs. – sbi Mar 05 '11 at 00:16
  • @sbi: well I certainly don't type 120wpm, so you win. If only I'd timed how long it took to type the comment we'd know how much by. But, your strategy would not have worked if it hadn't been a FAQ entry, so how to account for that risk? – Steve Jessop Mar 05 '11 at 00:20
  • Also, typing "c++-faq" into *my* browser's address bar doesn't take me to the SO tag, rather to Cline's FAQ-lite. So you win again :-) – Steve Jessop Mar 05 '11 at 00:21
  • @Steve: When a question looks like it has been asked many times before I always look first in the list of FAQs. If I can't find it, I consider adding one of the dupes to it. [This one](http://stackoverflow.com/questions/5200663/virtual-functions-iterating-over-a-vectorbase-class-that-is-populated-with-sub) is such a candidate, only it's 1:30am here and I got to go to bed now instead of hunting dupes. Oh, and my browser suggests SO's list of C++ FAQ first when I type `c++-faq` because I got to that page so often. `:)` – sbi Mar 05 '11 at 00:33

5 Answers5

39

It means that this function does not modify the observable state of an object.

In compiler terms, it means that you cannot call a function on a const object (or const reference or const pointer) unless that function is also declared to be const. Also, methods which are declared const are not allowed to call methods which are not.

Update: as Aasmund totally correctly adds, const methods are allowed to change the value of members declared to be mutable.

For example, it might make sense to have a read-only operation (e.g. int CalculateSomeValue() const) which caches its results because it's expensive to call. In this case, you need to have a mutable member to write the cached results to.

I apologize for the omission, I was trying to be fast and to the point. :)

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 5
    +1 for the "does not modify the observable state" formulation. Just to elaborate on that: any member marked as `mutable` _may_ still be modified by `const` functions. For that reason, `mutable` should only be used for internal bookkeeping that won't be "visible" to the outside world. – Aasmund Eldhuset Mar 05 '11 at 00:01
  • This answer misses an explanation on how the mechanism actually works, namely by simply changing the type of `*this` from non-const to const. The effects mentioned in the answer are therefore merely a consequence of `*this` becoming const. – Askaga Jul 08 '19 at 13:40
5

const at the end of the function means it isn't going to modify the state of the object it is called up on ( i.e., this ).

type CLASS::FUNCTION(int, const char*) const ; // Method Signature

type CLASS::FUNCTION(int var1, const char* var2) const {

}

You need to mention the keyword const at the end of the method definition too. Also note that only member functions can have this non-modifier keyword const at their end.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

Means that this function will not modify any member variables, or call any non-const member functions.

If you have a const reference/pointer to an instance of the class, you will only be able to call functions that are marked const like this.

EboMike
  • 76,846
  • 14
  • 164
  • 167
1

This means the method is const, and means the method will not modify any members, it can thus be used in a setting where the object is const.

class Foo
{
   public:
      void foo(int a) { m = a; }
      int bar() const { return m; }
   private:
      int m;
};

int baz(const Foo* ptr)
{
   ptr->foo(10); // Not legal, Foo::foo is not const, and ptr is pointer to const.
   return ptr->bar(); // Legal, Foo::bar is a const method, and does not modify anything.
}
Maister
  • 4,978
  • 1
  • 31
  • 34
0

A member function that inspects, i.e. reads, rather than modifies or writes to an object. The following link is helpful to me.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

Arun
  • 19,750
  • 10
  • 51
  • 60