-1

I'm learning C++ OOP. I understand functions and pointers (or at least I think so), but now I'm reading an example of a C++ class called "Student" and I found the following:

    void Student::modify_mark(double mark)
    {
      this->mark = mark;
    }

I know that this->mark is equivalent to (*this).mark, but I really don't get why we need a pointer in this case. Why not this.mark? We want to modify the mark of the student, right?

nohamk
  • 325
  • 2
  • 11
  • 2
    Because this is always a pointer. That's how the language is. (It does't require copies of the object, is more flexible than a reference, etc.etc.) – deviantfan Feb 24 '16 at 00:14
  • Yep but if it's a pointer by itself why do you have to call it as a pointer? I mean, if "this" is a pointer, then "*this" is a pointer to the pointer. My mind is about to blow. – nohamk Feb 24 '16 at 00:23
  • 1
    No, `*this` is the thing it points to. You are confusing it with `&this`, which would be a pointer to a pointer. – Jonathan Wakely Feb 24 '16 at 00:42
  • OK now I understand, thank you – nohamk Feb 24 '16 at 01:03
  • @deviantfan - What flexibility is gained through `this` being a pointer vs. a reference? Since it's defined as either `*const` or `const*const` I can't think of one. It can't ever be `nullptr` in any context that doesn't cause UB... – Edward Strange Feb 24 '16 at 17:53

3 Answers3

2

The short answer is: because.

In C++, this is a pointer. It has always been a pointer. It is what it is. Bears bear, bees bee, Spock beams up, and in C++ this is a pointer.

Could it have been a reference instead? Sure, I can't immediately see any technical reason why not. But, when C++ was a dream in Stroustrup's mind, he dreamed up this to be a pointer. If, by sheer luck, you ever run into him, you might want to ask him this question. You might get a fascinating answer, if so please share it with us, when that happens.

But since this was a pointer from the beginning, forever a pointer it shall be, until the heat death of the universe.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • For further reading: http://stackoverflow.com/questions/645994/why-this-is-a-pointer-and-not-a-reference – Tas Feb 24 '16 at 00:21
  • Great answer. Thank you, I thought that "this" as non-pointer element could also exist – nohamk Feb 24 '16 at 00:24
  • If @Jose runs into Mr. Stroustrup I hope they both come out OK. Hate to see people injured in pointless collisions. – user4581301 Feb 24 '16 at 00:45
0

Because this is a pointer.

As said in CPP reference:

The keyword this is a prvalue expression whose value is the address of the object, on which the member function is being called. It can appear in the following contexts: 1) Within the body of any non-static member function, including member initializer list 2) within the declaration of a non-static member function anywhere after the (optional) cv-qualifier sequence, including dynamic exception specification(deprecated), noexcept specification(C++11), and the trailing return type(since C++11) 3) within default member initializer (since C++11)

The type of this in a member function of class X is X* (pointer to X). If the member function is cv-qualified, the type of this is cv X* (pointer to identically cv-qualified X). Since constructors and destructors cannot be cv-qualified, the type of this in them is always X*, even when constructing or destroying a const object.

When a non-static class member is used in any of the contexts where the this keyword is allowed (non-static member function bodies, member initializer lists, default member initializers), the implicit this-> is automatically added before the name, resulting in a member access expression (which, if the member is a virtual member function, results in a virtual function call).

In class templates, this is a dependent expression, and explicit this-> may be used to force another expression to become dependent.

It is possible to execute delete this;, if the program can guarantee that the object was allocated by new, however, this renders every pointer to the deallocated object invalid, including the this pointer itself: after delete this; returns, such member function cannot refer to a member of a class (since this involves an implicit dereference of this) and no other member function may be called. This is used, for example, in the member function of the control block of std::shared_ptr responsible for decrementing the reference count, when the last reference to the managed object goes out of scope.

Paulo
  • 1,458
  • 2
  • 12
  • 26
-1

In C++ the this reserved keyword, is a pointer to the current class instance, therefore since it is a pointer, you must use ->

m_callens
  • 6,100
  • 8
  • 32
  • 54