tags:

views:

236

answers:

5

I am very confused about the const version and non-const version member function like below:

value_type& top() { return this.item }
const value_type& top() const { return this.item }

What is the difference between these two functions? In what situation would they be used?

+4  A: 

The const-qualified member function will be called if the member function is called on an object that is const-qualified.

The non-const-qualified member function will be called if the member function is called on an object that is not const-qualified.

For example:

MyStack s;
s.top(); // calls non-const member function

const MyStack t;
t.top(); // calls const member function

Note that the same rules apply when calling a member function on a reference to an object or through a pointer to an object: if the pointer or reference is to a const object, the const member function will be called; otherwise the non-const member function will be called.

James McNellis
Yongwei Xing
If an object is not const and there are both const and non-const overloads of a member function (and those overloads are otherwise the same), then the non-const member function will be called. You can "force" the const member function to be called by casting the object to a const reference and then calling the member function on that. A const member function cannot modify any non-mutable members of the object nor can it call any non-const member functions.
James McNellis
@Yongwei Xing, to add to James's response, if you give only a "const" overload, it will be used by both const and non-const objects. It's only if you have both a const and non-const overload, where they will use different versions. In general, if something can be declared "const", then it should be... non-const objects can invoke both const and non-const functions, while const objects can only invoke const-qualified member functions.
Michael Aaron Safyan
+2  A: 

If you have

class Foo
{
    value_type& top() { return this.item }
    const value_type& top() const { return this.item }
}

If you have

Foo foo;
const Foo cfoo;

The return types when you call top() are as follows:

value_type& bar = foo.top();
const value_type& cbar = cfoo.top();

In other words - if you have a constant instance of your class, the const version of the function is chosen as the overload to call.

The reason for this (in this particular case) is so that you can give out references to members (like item in this case) from a const instance of a class, and ensure that they too are const - thus unmodifiable and therefore preserving the const-ness of the instance they came from.

Andrew Russell
+12  A: 

In short, they're used to add 'const correctness' to your program.

value_type& top() { return this.item }

This is used to provide mutable access to item. It is used so you can modify the element in the container.

For example:

c.top().set_property(5);  // OK - sets a property of 'item'
cout << c.top().get_property();  // OK - gets a property of 'item'

One common example for this pattern is returning mutable access to an element with vector::operator[int index].

std::vector<int> v(5);
v[0] = 1;  // Returns operator[] returns int&.

On the other hand:

const value_type& top() const { return this.item }

This is used to provide const access to item. It's more restrictive than the previous version - but it has one advantage - you can call it on a const object.

void Foo(const Container &c) {
   c.top();  // Since 'c' is const, you cannot modify it... so the const top is called.
   c.top().set_property(5);  // compile error can't modify const 'item'.
   c.top().get_property();   // OK, const access on 'item'. 
}

To follow the vector example:

const std::vector<int> v(5, 2);
v[0] = 5;  // compile error, can't mutate a const vector.
std::cout << v[1];  // OK, const access to the vector.
Stephen
@Stephen: Thanks very much. I understand the difference between them now.
Yongwei Xing
A: 
const int* func() {}

means func() returns a const int*

int* func() const {}

means func() returns an int* and cannot alter any members in his class that are not defined as mutable. In this case, this is defined as a const type * const (constant type, constant pointer) instead of just a type * const (mutable type, constant pointer).

Dave
A: 

When member function is declared as const what's happening is that the implicit this pointer parameter passed to the function is typed to be a pointer to a const object. This allows the function to be called using a const object instance.

value_type& top();    // this function cannot be called using a `const` object
const value_type& top() const; // this function can be called on a `const` object
Michael Burr