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.