this - Why asterisk

Hi all!
I was reading an explanation about the this-pointer, and I came across the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

class Calc
{
private:
    int m_nValue;
 
public:
    Calc() { m_nValue = 0; }
    
    void printVar() { std::cout << m_nValue << std::endl; }
    Calc& Add(int nValue) { m_nValue += nValue; return *this; }
    Calc& Sub(int nValue) { m_nValue -= nValue; return *this; }
    Calc& Mult(int nValue) { m_nValue *= nValue; return *this; }
 
    int GetValue() { return m_nValue; }
};

int main()
{
    Calc cCalculate;
    cCalculate.Add(5).Sub(3).Mult(4);
    cCalculate.printVar();
    std::cin.get();
}


I don't understand why the asterisks are there at return *this
Could anyone explain that?
Thanks in advance!
It's the dereference operator.
http://en.wikipedia.org/wiki/Dereference_operator
I was reading an explanation about the this-pointer, and I came across the following code:

What is the most basic of the most basic facts about poinetrs? They are defined and their value is fetched by... suprise, suprise, the asterisk(dereference) operator!
Last edited on
The asterisk (*) is the dereference operator, it tells the function to return the object pointed by the pointer this. Any tutorial out there can explain pointers much better than I can, so here: http://www.cplusplus.com/doc/tutorial/pointers/

I also found the page you were reading, and they forgot to mention one thing: the use of the reference operator, or address-of operator (&) in the return type Calc&
http://msdn.microsoft.com/en-us/library/w7049scy(v=vs.71)

If you were to return a pointer (type Calc*) you could not synthesize the calls
1
2
3
cCalculate.Add(5); 
cCalculate.Sub(3);
cCalculate.Mult(4);

into
 
cCalculate.Add(5).Sub(3).Mult(4);

because a pointer behaves syntactically different than an object. For instance, to access m_nValue of cCalculate from the this pointer, or a pointer Calc* pointer; you would write this->m_nValue or pointer->m_nValue as opposed to cCalculate.m_nValue .
Also, if you were to return an object (type Calc), while it would syntactically allow the line cCalculate.Add(5).Sub(3).Mult(4); you would be modifying m_nValue of different objects of type Calc, as the functions would return copies of the objects.
This is where the reference comes in. A reference is pretty much a pointer that behaves syntactically like an object. It has the advantage that you can use it much like an object, but it can point to any object you assign it. That's why the use of the reference operator (&) in Calc&, it tells the function to return a reference to the object cCalculate, not a copy. As such, when the line cCalculate.Add(5).Sub(3).Mult(4); is executed, cCalculate.Add(5) will return a reference to the object cCalculate, which behaves exactly like an object does. Therefore, the line becomes cCalculate.Sub(3).Mult(4); , then cCalculate.Mult(4); and so on. All the calls to the functions modify m_nValue in the object cCalculate. Hence why they line cCalculate.printVar(); (which prints m_nValue of cCalculate) shows the correct result of (0+4-3*)4, which is 8.
It should be noted that the reference operator (&) has two uses (that I know of), the one previously explained and as address-of operator, which is explained in the pointers tutorial.

I hope I was of some help, if you did not understand something I'd be more than happy to give it another go.

P.S. I'm new to C++ and programming in general, so if my explanation or understanding of the concepts are erroneous I'd more than welcome a correction.



Thanks! I got it now. And @Euqirne VSR: You absolutely was of alot of help! Thanks for the effort!
Topic archived. No new replies allowed.