>>Q: shouldn't void poop() and void pee() return objects ?
It could, but you're voiding yourself of your feces and urine.
>>what exactly is, " this-> "
The keyword 'this' gives a pointer to the object that it's in. For example:
Code:
Person* Person::getPointerToTheObject()
{
return this;
}
int main()
{
Person p;
Person* ptr1;
Person* ptr2;
ptr1 = &p;
ptr2 = p.getPointerToTheObject();
return 0;
}
In this example, both ptr1 and ptr2 will contain valid pointers to the Person object called 'p'. I believe that's how cout works; its operator<< () is defined to return an ostream&, so what it does is:
1. Output whatever came after the <<
2. Return (*this) (this returns a pointer to itself; so de-reference the pointer and it returns itself).
That's why you can chain together a bunch of <<:
Code:
((((cout << "Hello! My name is ") << name) << " and I am ") << age) << "years old!";
cout << "Hello! My name is " returns cout, so the next step is cout << name, which returns cout, etc. and you get a nice chain reaction type thing going on
And to answer your question:
pointer->something()
is the same as
(*pointer).something()
So in short, -> is a shortcut that we use because we're lazy. Also, you can overload the -> operator, but that's not done very often so you probably don't need to worry about it. Putting it all together, this->doSomething() is basically saying (this current object).doSomething().