Yes, the Sam's book gets into:
static_cast (compile time)
dynamic_cast (run time)
reinterpret_cast (FORCE cast)
const_cast (work around on methods not set with const)
I have another 2 books & I started to read both partly, very early though.
5) I have another minor question from the different way you guys showed me to update operator++ (with "++*this;"). But maybe it is more of a gripe. And this is a great idea when you have tons of code there, which you can simply reuse from one operator to another.
1 2 3 4 5 6 7 8 9
|
Date operator ++(int) //Postfix ++Date
{
Date newDate(*this);
++*this; //works
//this->operator++(); //works
//++day; //works
PassPointer thisPP(this); //EXTRA TEST pass (&obj) to a pointer, which makes sense
return newDate;
}
|
"this" is = &object (address of the object), so can we say it is a pointer since it acts like one?
*this is = to the object itself by dereferencing.
How can I be sure when to use "this" vs "*this"
Here the pointer operator -> does the dereferencing:
|
this->operator++(); //works! (Has (&obj) and -> dereferences to use obj itself (and not address))
|
This line below works because the operator expects you to send the object itself, similar to "Date date1(date2);". The operator ++ cannot work directly with the address of the object (&obj).
If I make a function below & pass "PassPointer (this); from the class, then it will send the (&obj) which is exactly what the function needs, which is an address for the pointer.
[code]
void PassPointer(Date* copy){}
[code]
Sometimes I wish that you can just say "this" and have it be smart enough to extrapolate either the object or address of the object...I mean I gave you where the object is out figure it out compiler!!!
I am pretty sure this is for speed, rather than have it constantly checking behind the scenes, did they mean object or address.
When comparing to an int pointer it is comparable to "this":
int* pointer = (num1 = 22);
cout << pointer << endl; //(&obj) just like with "this"
cout << *pointer << endl; //22, the pointed to value.....(*this)(what it's pointing to, the obj itself!)