Quote:Original post by CodeStorm
There are a whole host of other reasons why one would also use pointers over objects, such as passing arguments to a function (it's usually MUCH cheaper to pass a pointer than a large object for instance, however, for single objects, passing references is usually safer), but at least I've given you a heads up:
I just want to point out that most implementations of references are done using pointers. That means that, practically, passing a parameter as a pointer is, at a low level, the same as passing a reference. I usually use references when the parameter's type is a not a built-in type and is a required parameter. If the parameter is "optional", I use pointers so the caller can pass NULL instead.
Quote:If you want to control the lifetime of the object, create it on the heap, otherwise dump it on the stack, which will delete the object automatically when it is no longer needed.
I can control an object's lifetime in the stack as easily as in the heap by using the objects in the correct scope. Actually, I would say that you want to use pointers when you
can't control an object's lifetime, such as when creating an object inside a function and returning said object to the caller. That is, when you want to defer the deletion of the objects at some point later in the program's execution and outside the current scope.
Quote:I strongly recommended that you read a decent book on the subject of pointers. It's an imperative skill if you wish to master C/C++.
Seconded. I don't want to hurt any feelings, but if you can't seem to find a situation where pointers are necessary or desirable, then you haven't used them or don't understand them enough. I also would say that
avoiding the use of pointers as much as possible is a good strategy, but that is more an style than anything else (I've seen working applications that only use built-in types and pointers. No copy and no references anywhere.)
[Edit: formating]