2

There is a class A and it has the following operator() implementation:

void A::operator()(...parameters...) const 
{
    // operator body
}

What does this const mean?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Alex
  • 9,891
  • 11
  • 53
  • 87

6 Answers6

11

Methods in C++ can be marked as const like in the above example to indicate that the function does not modify the instance. To enforce this, the this pointer is of type const A* const within the method.

Normally, the this pointer within a method is A* const to indicate that we cannot change what this points to. That is, so that we cannot do this = new A().

But when the type is const A* const we also cannot change any of the properties.

Cam
  • 14,930
  • 16
  • 77
  • 128
  • The reason that you can't change `this` is because it is not an _lvalue_ expression, not because it has a `const` qualified type which it doesn't. In fact you cannot have _rvalues_ of `const` qualified non-class types. – CB Bailey Mar 20 '12 at 17:47
  • @CharlesBailey: Where did you find that information? The sources I've consulted explain that the pointer is of type `const A* const`: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr035.htm http://msdn.microsoft.com/en-us/library/ytk2ae82.aspx – Cam Mar 20 '12 at 18:12
  • In the standard, section about `this` in chapter 9 and section about value categories in chapter 3. – CB Bailey Mar 20 '12 at 18:31
  • 1
    @CharlesBailey: Well that's certainly the most definitive source. Thanks for correcting the info I provided. For others interested, see http://www.cs.technion.ac.il/users/yechiel/CS/C++draft/ISO-CPP-body.pdf page 155. Thanks Charles! – Cam Mar 20 '12 at 18:38
  • However, just to confuse the issue, const member functions can modify members explicitly marked as `mutable`. – Ferruccio Mar 20 '12 at 20:14
2

That the method uses this as a const A* and then can only call other const methods.

See this entry of the CPP FAQ.

Luca Martini
  • 1,434
  • 1
  • 15
  • 35
  • 2
    Small nitpick - the type actually becomes `const A* const` – Cam Mar 20 '12 at 16:39
  • @Cam: no, the type is simply `const A*`. `this` is an _rvalue_ which is why you can't assign to it or take its address, not because it's `const`. – CB Bailey Mar 20 '12 at 17:39
1

As already amply described, it means that the method will not modify the observable state of the object. But also, and very importantly, it means the method can be called on a const object, pointer, or reference - a non-const method cannot. ie:

class A
{
public:
    void Method1() const
    {
    }

    void Method2()
    {
    }
};

int main( int /*argc*/, char * /*argv*/ )
{

    const A a;
    a.Method1(); //ok.
    a.Method2(); //compiler error!

    return 0;
}
Grimm The Opiner
  • 1,778
  • 11
  • 29
  • please do not assume that it won't modify the object, only the visible state remains unchanged. – Sebastian Mach Mar 20 '12 at 17:01
  • Also already amply stated. : ) So far I'm the only person to point out that only const methods can be called on a const object. Where's my upvotes!?!? – Grimm The Opiner Mar 20 '12 at 17:15
  • actually, you have an upvote from me, but also a downvote, because const methods may change the object. But they shall not change the externally visible state. – Sebastian Mach Mar 21 '12 at 15:41
  • I thought it was pretty clear that I was summarising the answers above, before adding the important bit. I've now polished it. Still, now I must find the downvoter and stalk them across the internet forever! Where am I gonna find the time for that!? – Grimm The Opiner Mar 21 '12 at 15:55
  • You want to use '@' next time, so I will be notified ;) My (virtual) downvote vanishes, summing to +1. Btw, you've not received any real downvote. – Sebastian Mach Mar 22 '12 at 12:20
0

const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.

DotNetUser
  • 6,494
  • 1
  • 25
  • 27
0

It means that calling the operator will not change state of the object.

piokuc
  • 25,594
  • 11
  • 72
  • 102
  • Not necessarily, thanks to `const_cast` and `mutable`. – dan04 Mar 20 '12 at 16:36
  • 1
    @dan04: Ok, so it means "will not change state of the object... unless someone cheats!" :P – aldo Mar 20 '12 at 16:41
  • @dan04: mutating a const value via `const_cast` yields undefined behaviour. – Sebastian Mach Mar 20 '12 at 16:43
  • @piokuc: Maybe refine this to "observable state". – Sebastian Mach Mar 20 '12 at 16:44
  • Sure, yes, in general it does not have to be true, but I just wanted to give an answer which would be more useful for a novice C++ programmer than 'it means that type of this will be const A* const in the function'. – piokuc Mar 20 '12 at 16:47
  • @phresnel What's the difference between state and observable state? – piokuc Mar 20 '12 at 16:49
  • 1
    @piokuc: Example: You have a quadtree class. Under the hood, the quadtree is build up lazily, i.e. on demand; this lazyness is hidden from users of the class. Now, a query-method on that quadtree should be `const`, because querying a quadtree is expected to not change the quadtree. How do you implement const-correctness and lazyness without breaking C++' rules? Use `mutable`. Now you have a quadtree that for the user looks const, but you know that under the hood, there's still a lot of change. – Sebastian Mach Mar 20 '12 at 16:52
  • @phresnel Thanks for that, I wasn't aware of the idea of 'observable state' even though I knew about mutable keyword. I have some doubts about const-correctness achieved this way - is it worth the trouble if you know the compiler cannot check it? I mean, if you "cheat" with const_cast the compiler is aware of it, which in theory can help it in taking some optimization decisions, whereas I don't think it is possible to prove that modification to mutable members does not change the observable state, is it?... Thanks, anyway. – piokuc Mar 20 '12 at 17:09
0

A const member function promises to not change the observable state of the object.

Under the hood, it might still change state, e.g. of mutable members. However, mutable members should never be visible from anywhere outside the class' interface, i.e. you should never attempt to lie to client code, because client code may validly expect that const functions don't tweak the object's outer hull. Think of mutable as an optimization tool (e.g. for caching and memoization).


Example for mutability:

You have a quadtree class. Under the hood, the quadtree is build up lazily, i.e. on demand; this lazyness is hidden from users of the class. Now, a query-method on that quadtree should be const, because querying a quadtree is expected to not change the quadtree. How do you implement const-correctness and lazyness without breaking C++' rules? Use mutable. Now you have a quadtree that for the user looks const, but you know that under the hood, there's still a lot of change:

class Quadtree {
public:
    Quadtree (int depth);
    Foo query (Bar which) const;

private:
    mutable Node node_;
};
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130