6

What is the difference between

    const string& getName() const {return name;}

and

    string& getName() const {return name;}

What does const mean at the beginning and at the end?

Mat
  • 202,337
  • 40
  • 393
  • 406
Laura
  • 131
  • 3
  • 9
  • http://www.cprogramming.com/tutorial/const_correctness.html this is a good tutorial on how to use const – Lucian May 07 '12 at 12:56
  • possible duplicate of [Meaning of "const" last in a C++ method declaration?](http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration) – juanchopanza May 07 '12 at 13:05
  • possible duplicate of [How many and which are the uses of "const" in C++?](http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c) – dmckee --- ex-moderator kitten May 07 '12 at 16:55

4 Answers4

9

The const at the end of the function signature means the method is a const member function, so both your methods are const member functions.

The const at the beginning means whatever is being returned is const.

The first example is a const method returning a const reference to internal data, and is therefore const-correct.

The second is a const method returning non-const reference to internal data. This is not const-correct because it means you would be able to modify the data of a const object.

A call to a const a method cannot change any of the instance's data (with the exception of mutable data members) and can only call other const methods.

Const methods can be called on const or non-const instances, but non-const methods can only be called on non-const instances.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
7

One returns a const reference and is a const member function , the other is a const member function.

const string& getName() const {return name;}

the returned string can't be modified, and the method is const (see below).

string& getName() const {return name;}

the method can't modify non-mutable class members.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

We use const in the prefix of the function to ensure whatever is getting returned from the function becomes constant.

And we use const as the postfix in the function declaration to ensure that the function does not do any modification in the current instance of class i.e if the member function of the class tries to modify the current object's attribute's value then the compiler will throw an error.

const function is mainly used for const object to ensure that the function is not trying to modify current object data.

Adarsh Kumar
  • 467
  • 6
  • 11
1
const string& getName() const {return name;}

return const reference means that you will not be able the instance after returning the reference to it.

string& getName() const {return name;}

const method means that this method will not change the state of the object except the mutable member variables. Also this method can be called on an const object for example:

class MyClass(){
public:
  void doSomethingElse()const;
  void notConstMethod();
};

void doSomething( const MyClass& obj ){
  obj.doSomethingElse();
  obj.notConstMethod(); //Error
}
jrok
  • 54,456
  • 9
  • 109
  • 141
AlexTheo
  • 4,004
  • 1
  • 21
  • 35