why one would define an object of a class as a pointer
One shouldn't. Pointers are generally to be avoided as much as possible. However, they are necessary when you are doing polymorphism. In that case, use the smart pointers shared_ptr, unique_ptr instead of a raw pointer.
The following is a bad example of using pointers because now you have one additional problem i.e., "freeing the allocated memory".
int main() { Car *c = new Car; c->accelerate(); return 0; }
And you are right, the second example is much better and should be the default way to go.
Whenever such questions occur, it is best to see what C++ Core Guidelines say:
Reason
The pointer returned by new should belong to a resource handle (that can call delete). If the pointer returned by new is assigned to a plain/naked pointer, the object can be leaked.
Reason
Direct resource management in application code is error-prone and tedious.
Reason
There is nothing (in the C++ standard or in most code) to say otherwise and most raw pointers are non-owning. We want owning pointers identified so that we can reliably and efficiently delete the objects pointed to by owning pointers. (Owning pointers are pointers which take ownership of a pointer and are responsible for freeing it.)
Example
void f() { int* p1 = new int{7}; // bad: raw owning pointer auto p2 = make_unique<int>(7); // OK: the int is owned by a unique pointer // ... }
So, the answer is use pointers only when you absolutely need to, otherwise stick to references and values.
When to use pointers?
- When you are doing polymorphism (use smart pointers)
- When you are need a huge array (> 1MB) because stack size is limited. (2 - 8 MB(usually) on linux, 1 MB on windows). Prefer using
std::vector in this case if you can. - Pointers can sometimes be necessary when you are using "C" libraries or dealing with legacy C++ code.