2

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

I have seen some classes that have something like this.

void something() const;

What does const means?

Community
  • 1
  • 1
Ramilol
  • 3,394
  • 11
  • 52
  • 84
  • 1
    @David Maybe the OP did google but still didn't understand? Maybe she is looking for a more "laymen" view of an answer and less of a textbook answer? Or maybe the OP is just lazy - who knows - but that's what SO is good for right? :) – SRM Dec 23 '10 at 17:19
  • 1
    @SRM yeah maybe, but I do think that, in general, a lot of people asking Qs on SO could do a better job of using a web search. At the very least to state that they did a web search, learnt such and such, but were unsure about this and that would be likely to engender a bit more enthusiasm to spend time answering. – David Heffernan Dec 23 '10 at 17:24
  • @David I agree. As a matter of fact, when I do my own researching and I google for something 9 times out of 10 I find a relevant SO article. I've gotten to the point of just searching SO first, and bearing no fruit turning to google. As my final resort I will post a question here, but, yeah, in general google is the place to start. – SRM Dec 23 '10 at 17:27
  • @SRM my no. 1 top tip for anyone asking a question here on SO is to learn how to do a web search. It's actually not a trivial skill and it frequently amazes me how many people cannot do it. – David Heffernan Dec 23 '10 at 17:29
  • @David I agree wholeheartedly. Massaging google for answers is more an art than science. It's all about figuring out the right keywords that will give you the relevance you want. That also forces you to frame your question as narrow as possible. All good things when you are researching or asking for guidance. – SRM Dec 23 '10 at 17:32
  • just to let you know i'm he not a she.... – Ramilol Dec 24 '10 at 01:03

5 Answers5

5

In addition to what SRM said (that the function cannot alter any members of the class (which is not exactly true, e.g. in the case of mutable members, which still can be altered)), it means that you can call this member function on things that are const. So if you get a Foo const& foo as a parameter, you can only call members that are declared const like in your question.

etarion
  • 16,935
  • 4
  • 43
  • 66
  • good point. I've modified my answer to reflect those nuances. Also upvoted your answer. – SRM Dec 23 '10 at 17:17
4

It means that something may not modify member variables of the class. The exception to the rule is if there are member variables declared with the mutable keyword.

For example, suppose you have a std::map< int, int > var member variable and a method that does the following:

int Class::method () const {
    if (var.find (42) != var.end ()) {
        return var[42];
    }
    return 0;
}

That will not compile since var[42] modifies var if 42 is not in the container. Declaring var as mutable allows you to pass compilation.

ezpz
  • 11,767
  • 6
  • 38
  • 39
  • 1
    `this` can never be modified - it's an _rvalue_ expression. What's different in `const` member functions is that the type of this is a pointer to const class type. – CB Bailey Dec 23 '10 at 17:16
  • Thank you. Updated accordingly. – ezpz Dec 23 '10 at 17:30
3

It means the function will not modify any member variables. To be more precise, it means the function cannot alter any non-static or immutable member variables of that class

SRM
  • 1,377
  • 1
  • 10
  • 17
  • It's still free to modify static members and members declared mutable. – Ferruccio Dec 23 '10 at 17:13
  • 1
    Anything is a bit strong, also it doesn't necessarily mean that no member variable can be edited it just makes `this` a pointer to `const` so members can't be modified through `this` (either explicitly or implicitly). Consider `class x { int y; void f( int& z ) const { ++z; } void g() { f( y ); } };` – CB Bailey Dec 23 '10 at 17:15
  • @Charles and Ferrucio - Excellent points. I've modified my answer to be more precise. – SRM Dec 23 '10 at 17:16
1

To be absolutely precise - it makes the implicit this pointer in the function a pointer to a const object.

From 9.2.1 "The this pointer""

The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

All the other behaviors (that you can't modify the object members, that you can call the function using a const object, etc) fall from that.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

it declares that this function can not change any data. Makes it a Read Only Function.

Saad Masood
  • 11,036
  • 9
  • 32
  • 40