2

in

int salary() const { return mySalary; }

as far as I understand const is for this pointer, but I'm not sure. Can any one tell me what is the use of const over here?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
nik
  • 8,387
  • 13
  • 36
  • 44

7 Answers7

7

Sounds like you've got the right idea, in C++ const on a method of an object means that the method cannot modify the object.

For example, this would not be allowed:

class Animal {
   int _state = 0;

   void changeState() const { 
     _state = 1;
   }
}
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
  • This syntax is only valid for C++0x – ergosys Jan 08 '10 at 03:06
  • Which syntax? I haven't typed any C++ in a while, so perhaps I got something slightly wrong, no guarantees it compiles, but overlooking any mistakes I'm sure you get the general idea, and the general idea is part of regular C++. – waterlooalex Jan 08 '10 at 03:09
  • I could be mistaken, I was using MSVC++ version 6 (years ago), perhaps it didn't conform to the spec, does Microsoft ever do anything like that? ;) – waterlooalex Jan 08 '10 at 03:12
  • Under C++-98: §9.3.3: "A nonstatic member function may be declared const, volatile, or const volatile. These cv-qualifiers affect the type of the this pointer (9.3.2)." Therefore, that code snippet should produce an error. – greyfade Jan 08 '10 at 03:38
  • 1
    I was referring to the instance member initialization, this is new for C++0x. – ergosys Jan 08 '10 at 04:16
  • Alex has been doing C#, I'd guess. But the idea is the same. – Hans Passant Jan 08 '10 at 04:59
  • @ergosys: ah, thanks. I've been doing C# and Scala recently, the C++ knowledge is getting overwritten! – waterlooalex Jan 08 '10 at 13:34
  • Note: any member variables marked `mutable` can be changed through a `const` method: http://stackoverflow.com/questions/105014/c-mutable-keyword – Matt K Jan 03 '12 at 18:00
5

When the function is marked const it can be called on a const pointer/reference of that class. In effect it says This function does not modify the state of the class.

Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
0

A const after function of a class, means this function would not modify any member objects of this class. Only one exception, when the member variable is marked with Mutable.

Pengzhi Zhou
  • 481
  • 1
  • 6
  • 24
0

It's a const method. It means that it won't modify the member variables of the class nor will it call non-const methods. Thus:

const foo bar;
bar.m();

is legal if m is a const method but otherwise wouldn't be.

jason
  • 236,483
  • 35
  • 423
  • 525
0

It means that function can be called on a const object; and inside that member function the this pointer is const.

Terry Mahaffey
  • 11,775
  • 1
  • 35
  • 44
0

It just guarantees that calling salary() doesn't change the object state. IE, it can be called with a const pointer or reference.

user207462
  • 81
  • 4
0

It's a const member function. It's a contract that the function does not change the state of the instance.

more here: http://www.fredosaurus.com/notes-cpp/oop-memberfuncs/constmemberfuncs.html

mmattax
  • 27,172
  • 41
  • 116
  • 149