References and Pointers (when to put them to use?)

If anyone can give me some examples of situations that you should use references or pointers in your programs... I'm not sure when to use them.
Thank you
These may help you:
pointers
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/
http://www.cplusplus.com/doc/tutorial/polymorphism/
references
http://www.cplusplus.com/doc/tutorial/functions2/ (just first section)

Pointers can be used in lots of ways.
References are usually found as function parameters, when you want to modify the argument or when you don't want to copy it
eg:
1
2
3
4
5
6
7
8
9
void set_to_five ( int &n )
{
     n = 5; // this would change the value of the variable passed as argument
}


int x = 1;
set_to_five ( x );
// now x == 5  


 
void some_function ( const Class &obj ); // here you won't call the copy constructor for 'Class', useful when 'Class' is complex to copy ( or when it hasn't copy constructor ) 
Topic archived. No new replies allowed.