32
struct Foo
{
 void f()
 {
 // (*)
 }
};

What is the type of "this" in the line marked with (*) ?

Is it const Foo* or Foo* ?

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • 5
    It's probably `Foo * const` – Tom Knapen Jun 17 '13 at 11:34
  • 8
    @TomKnapen no it's not: http://stackoverflow.com/questions/16102143/c-the-this-pointer-is-always-const/ – Arne Mertz Jun 17 '13 at 11:39
  • 1
    If it were `const Foo *`, that would imply you can't change the member variables in the object (imagine all the accesses having the `this->` stated explicitly). That's not the intent unless you declare the member function itself `const`. –  Jun 17 '13 at 11:51
  • 1
    Another voice: http://stackoverflow.com/a/6067267/1715716 – Gauthier Boaglio Jun 18 '13 at 12:47

4 Answers4

70

n3376 9.3.2/1

In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called.

The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • 2
    That makes me wonder why they didn't make `this` a const pointer... Any particular reason? – Tom Knapen Jun 17 '13 at 11:41
  • 17
    @TomKnapen `this` is an r-value, so you can't modify it anyway. For most intents and purposes, it behaves as `X * const`. But you can't take its address, for example. – Angew is no longer proud of SO Jun 17 '13 at 11:47
  • 3
    @TomKnapen: No. In hindsight, it should have been a reference, not a pointer. But `this` predates references, and the C++ tradition of not breaking existing code is really old. – MSalters Jun 17 '13 at 13:36
  • @TomKnapen it makes no sense for a value to be const. What can be changed? Can a value be changed to be something else? Or can an object to be changed to contain some other value? It is the former, and the former only can be const (it has a *lifetime*, during which it gets no different value). – Johannes Schaub - litb Jun 17 '13 at 14:00
  • The type of an expression picks up the constness of the object, and will then be of a const type. See the note on 4.1p1 – Johannes Schaub - litb Jun 17 '13 at 14:14
5

Inside f, this has type Foo * because f is not a const member function.

You cannot call f on a const Foo object. The following is erroneous:

 const Foo obj;
 obj.f();

This is precisely because inside Foo::f, the this pointer is Foo * rather than const Foo *, and so the function call demands a pointer conversion which discards a qualifier.

The this pointer itself is not a variable. It is not assignable, but not because of a const qualifier. There is no declaration in scope such as Foo *const this. A this expression is simply not an lvalue, as a rule of the language.

The this pointer is not very different from &obj.

Kaz
  • 55,781
  • 9
  • 100
  • 149
3

The type of this depends on the member function.

For example for a class X, if the member functions is

1) const, Then this is of type const X*

2) volatile, then this is volatile X* etc

otherwise it is X*

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
-5

this refers to current object. The type of this in c++ is Foo *const.

suspectus
  • 16,548
  • 8
  • 49
  • 57
vinod
  • 132
  • 5