Pointers and references?!? Help!

This is a discussion on Pointers and references?!? Help! within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been studying C++ for a few months now.. However, I just couldn't grasp completely how to apply pointers ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question Pointers and references?!? Help!

    Hi,

    I've been studying C++ for a few months now.. However, I just couldn't grasp completely how to apply pointers and references in codes. I think I understand how they work, though. I am having trouble with using it in my own functions. I am getting desperate! I tried to move on to more advanced subjects like classes and operator overloading, hoping that the idea of pointers and references will eventually sink in. But I guess, I needed to fully grasp those topics first since the knowledge of it is essential to other subjects.

    Any suggestions on how I can understand it better?

    Much appreciated..

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What don't you understand about them?

    Use references whenever possible, rather than pointers.
    References cannot point to anything else after you initialize them, so if you always want it to refer to the same object and the object wasn't dynamically allocated, references are definitely the way to go.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Pointers have useful applications when allocating things on the heap, changing data located elsewhere, and also building your own data structures. References can also change data located elsewhere, because when you make a reference you are giving some datum an alias.

    For example, you might see a function that accepts a reference to avoid making unnecessary copies of an object.

    size_t length ( const string & val );

    And you might see the same function implemented with pointers to support c-strings.

    size_t length ( const char * val );

    Both of these functions use data located elsewhere in the code and being able to refer to that data indirectly is part of pointers and references usefulness.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Can you write a function to swap the value of two variables? You can do this using either pointers and references.

    Passing by const reference is often used with classes to avoid making an unnecessary and potentially expensive copy of the object (e.g most of the time std::string would be passed by const reference).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 01:29 PM
  2. Pointers v References
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 06-30-2008, 01:29 PM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21