tags:

views:

236

answers:

7

Please help me understand the following signature:

err_type funcName(const Type& buffer) const;

so for the first const, does that mean the contents of Type cannot change or that the reference cannot change?

secondly, what does the second const mean? I don't really even have a hint.

Thanks in advance, jbu

+14  A: 

The second const means the method can be called on a const object.

Consider this example:

class foo
{
public:
    void const_method() const;
    void nonconst_method();
};

void doit()
{
    const foo f;

    f.const_method();      // this is okay
    f.nonconst_method();   // the compiler will not allow this
}

Furthermore, a const method is not allowed to change any members of the object (unless the member is specifically marked at mutable):

class foo
{
public:
    void const_method() const;

private:
    int r;
    mutable int m;
};

void foo::const_method() const
{
    m = 0; // this is okay as m is marked mutable
    r = 0; // the compiler will not allow this
}
R Samuel Klatchko
Very clear explanation!
Jim Schubert
And you should probably not have mutable members of your class.
Martin York
Mutable members are very useful. The compiler only enforces bitwise constness -- meaning that the actual bits of the object can't change . However, if the logical state of the object is not affected by the change, the item should be declared mutable. For example, if I have an object that queries a database, it makes sense for a const object to allow select queries against itself. But it might need to modify, say, a "database_connection" object internally while running a function, which may be reused in other queries, In that case, `mutable` helps you maintain logical const -- a good thing.
Billy ONeal
+2  A: 
Roger Pate
+5  A: 

Yes, the first const means that buffer can not change.

The second const implies that this is a class' member function, and will not change the object (this).

Shmoopty
+4  A: 

This will explain better than most of us probably can so I wont repeat: http://www.parashift.com/c++-faq-lite/const-correctness.html

Zac
+1  A: 

The other answers are correct, but it has a more significant meaning. It is a contract for your class that says "by calling this function, you will not change the state of your object". People interpret this differently, some view it as the object will not change in appearance to the caller, while others view it as the object will not change at all if you call this function.

Steve
A: 

The first const is a gurantee that the function will not change buffer, and thus can be passed a const buffer.

The second const is only meaningful when used as a method for a class. It is a gurantee that calling the method will not have any side effects on the class that contains the method. Essentially a gurantee that this method can be safely called on const objects.

John Knoeller
To be precise 'will not have any effects on the class that contains the method' should be reworded. A constant method can change static members of the class, what must remain unchanged is the instance on which the method is called.
David Rodríguez - dribeas
A: 

While you've gotten some good answers, I think it's worth pointing out that a class can contain mutable member variables. A mutable member is one that you're always allowed to modify, even when it's part of a const object. These are typically used for things like memoizing and caching, where the variable can be modified without affecting the user-visible state of the object as a whole.

Jerry Coffin