the memory address that was created on the heap for this object is cleared. However, if I were to write:
1 2 3 4 5
void CreateCat()
{
Cat cat;
cat.Meow();
}
This object has its memory created on the stack right? or am I incorrect? Either way, I wanted to make sure that the memory allocated for this object is cleared automatically when I exit the scope of the function CreateCat().
Your second example is the best way to deal with it -- it is a temporary which is created on the stack.
There are cases where you need to create temporaries on the heap. For example, large objects are best constructed on the heap. In that case, it is best to use a std::auto_ptr.
Instead of your first example, you would do something like this:
OK. Thank you! Another question though. Why use references? Aren't pointers, while slightly more annoying, more powerful? Having said that, other than passing in pointers/references through functions, why use them?
Pointers are indeed more powerful, and there are many situations where pointers do things references can't do. References work as syntactic sugar for all the other situations.
For example, a common case is passing an std::string by reference/pointer without accepting null pointers. You can either pass a const std::string * and check whether the pointer is valid, or just pass a const std::string & and make the check unnecessary (and impossible).
Basically, the question of why to use references is the same as why to use the [] operator when you can just do *(pointer+offset), or why use -> when you can just do (*pointer).member.
My current understanding of the usage of pointers and references is that it is both permissible and recommended to pass pointers/references when the object in question is large and/or will not be modified (unless the modification of said object is intended).
You pass by reference* when the object you want to pass won't be modified in the caller's stack frame but is too large to copy (e.g. a string or a vector) or when it needs to be modified in the caller's stack frame regardless of its size.
*Passing by reference is a CS term that means passing data to a function in such a way that the modifications applied in the callee are applied also in the caller. In C++ terminology, it means passing either a pointer or a reference.