CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Reference and const pointer

    they're different types. and they're mostly interchangable at the code level.

    the differences being:
    pointers can point to "anywhere", including null (and it's easy to make a pointer point to anything you want).
    pointers can be initialized and assigned

    references "point to" objects. making a reference NOT "point to" an object typically involves a detour over a pointer which sidesteps the problem. So for the most part it is easier to make "clean" code with references.
    references can only be initialized (i.e. you can't change a reference, you can only change what a reference "points to".).

    pointers tend to confuse programmers, especially new programmers. It also opens the question as to "who owns the pointer, and do I need to dispose of the pointer when I'm done with it".

    the rule of thumb should be:
    Use references. Only use pointers when a reference won't work.
    Last edited by OReubens; June 11th, 2013 at 04:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured