Hello,
I'm quite confused by pointers and references being passed/returned to a function .
I think I understand passing values as a pointer or as a reference.
I would like to see some examples in which
* a value is returned as a pointer
* a value is returned as a reference
* a pointer is passed as a pointer , and returned as a pointer
* a pointer is passed as a pointer , and returned as a reference (is that
even possible?)
* a reference is passed as a pointer and returned as a pointer
* a reference is passed as a reference and returned as a pointer(possible?)
* a reference is passed as a reference and returned as a reference?
And what would be a reason for using these passing/return methods?
Thanks.
So pPet is a pointer to Animal , yet it can't access the Cat::eat() method ? Why not , it's new Cat ?
Yes, but you assigned it to a pointer to Animal, so the compiler downcast it from a Cat to an Animal. Cat's unique methods are no longer available. Note that since eat() is overloaded, a call to pPet->eat() will invoke Cat's eat() method.
And so why not just use the panimal or pCat type pointers ?
Let's assume you also had a Turtle class derived from Animal which also has an eat() method.
Now, you can call eat() through a pointer to Animal without caring which kind of animal it is. The correct eat() function will be called.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.