Why using pointers ?

Hey guys , so I've been learning c++ for a while now , and since I just finished finals , I tried to review what I took and then learn more , the problem is , this question got in to me :(

why using pointers ?

I can still use normal variables and if I wanted to modify it I could use & operator to pass it to a function to get modified.

So why ? please try to explain it like trying to explain something to a child because I'm new to c++ :D

Thank you sir , I was just reading this , but he said " when you can't use anything else " how so ?

Also I opened the book and tried to go over pointers again , and it says pointers help to manage the memory , where you could use it to tell how many space does a thing needs in the runtime not in the compile time, is this why it's important ?
If you're new then there's a chance you might not see the full merit of them just yet.

Just wait until you try and make use of heap memory or write something polymorphic. :-)
I think I'll follow your advice and wait until I get a little better and learn more :D

thank you guys ^^
> 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/
See my post in the following thread for more reasons why you might want to use pointers:

http://www.cplusplus.com/forum/general/105593/
Thanks guys , I think i'll stick with learning new things for now , and I'm sure when I need to use them I'll know.

Thanks for helping me ^^
Topic archived. No new replies allowed.