> but he said " when you can't use anything else " how so ?
When you are writing a program, you would know when you can't do without pointers. Wait till you actually encounter a situation where pointers are indispensable. Till then, do not use them; program as if pointers do not exist.
In general, this would be the order of preference:
a. If we can use values, we should simply use values.
1 2 3 4
|
int i = 7 ;
int foo( int a ) ;
struct book { std::string title ; int pages ; };
int bar( book b ) ;
|
etc.
If we need to refer to objects indirectly, values won't do. Then:
b. Consider if we can use an alias (a simple reference).
c. If the alias needs to be changed (refer to different objects at different times), use
std::reference_wrapper<>
d. If none of the above applies (an object may or may not be there, we need to refer to unnamed objects that are allocated dynamically etc.), finally fall back to using a pointer.
Even in this situation, if possible use wrappers (
std::string,
std::vector<> etc) that hide pointers as implementation details.
This question has been raised many times; search and you would find many other threads dealing with the same topic. For instance, this one:
http://www.cplusplus.com/forum/general/114879/