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

Thread: Reference and const pointer

  1. #1
    Join Date
    Jul 2007
    Posts
    227

    Lightbulb Reference and const pointer

    Hi,
    what is the advantage of using reference over constant pointer?
    Can they be used interchangeably?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Reference and const pointer

    The biggest difference is a pointer can be null

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,878

    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.

  4. #4
    Join Date
    Jul 2007
    Posts
    227

    Re: Reference and const pointer

    Thanks.
    So is it wrong to say Reference is constant pointer?

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Reference and const pointer

    Yes, that would be incorrect.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Reference and const pointer

    Quote Originally Posted by Rajesh1978 View Post
    Thanks.
    So is it wrong to say Reference is constant pointer?
    Yes. OReubens explained the difference quite clearly, I though.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,878

    Re: Reference and const pointer

    If you really have to... you could say that a reference "behaves like" a dereferenced const pointer to an object.

    saying a reference IS a constant pointer is wrong because:

    * they aren't equal, they're different types, so "IS" in that sentence is wrong. you access a member from a reference via the 'dot operator' or "object member selection operator" to use the proper term as in ref.member, where you access a member of a const pointer via the 'arrow operator' or "pointer member selection operator" as in ptr->member.
    So you use them differently in code. If you change a function prototype from a reference to a const pointer you will also need to change all the indirections for the code to compile and work properly.

    * so to extend the above, saying it's like a dereferenced const pointer (*ptr) you could use the in a similar manner as in (*ptr).member being equivalent to ref.member

    * then there's still the matter that a const pointer still has ambiguity in who owns the pointer (do you need to delete the pointer ?) and that the pointer value may not point to an actual object. so you may need to test against NULL (and/or other specialty values).


    also note that very few code gets written that has a const pointer (you do see '(variable) pointers to const something' a lot) and I'd even guess a lot of c++ programmers would be confused at the syntax thereof. Again, more reason to avoid pointers where possible and use references instead.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center