CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    References versus pointers

    Hi,

    If I want to pass the address of something to a function, why would it ever be better to use a reference instead of a pointer ? I'm aware of two possible reasons :

    1 : I know that the called function is more legible if I use a reference. (I don't have to dereference a reference :-))

    2 : If I decide that the called function requires not the address of the "something" but the value of the "something", I can make the change simply in the the function prototype.

    What other advantages are there ?

    Regards,

    Brendan


  2. #2
    Join Date
    May 1999
    Posts
    19

    Re: References versus pointers

    One advantage of using a reference is that you won't be passed a NULL pointer.

    Another advantage would be in easier coding of the called function, i.e. you don't have to dereference the pointer.

    -- Matt


  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: References versus pointers

    Copy constructor:

    class A {
    public:
    A(const A&);
    };

    Regards,

    Paul McKenzie


  4. #4
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: References versus pointers


    Remeber that a reference in essence is a pointer, but viewad as a variable
    A reference is a pointer that can NEVER change its value and can NEVER be NULL

    refeneces are GREAT to use

    sally


  5. #5
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: References versus pointers

    Hi Sally,

    Thanks for the input.

    >>A reference is a pointer that can NEVER change its value....

    Are you saying that a reference can only ever be the address of one thing ?

    Regards,

    Brendan


  6. #6
    Join Date
    Apr 1999
    Posts
    90

    Re: References versus pointers

    One of things to remember about references is that you have to pass the correct data type. Don't assume implicit type casting will work correctly.

    For example, if you have the function:
    void SomeFunction(long int &nVal);

    Don't do this!:
    short n;
    SomeFunction(n); // passing a short for a reference to a long may be a problem



  7. #7
    Guest

    Re: References versus pointers

    Hi,
    Having passed the parameter as a pointer, not only the contents of that memory can be changed( which is reflected in the called function) but also the pointer can be made to point to any other memory location. Thus the pointer value ifself can be changed .
    But having passed the parameter as a refernce , the pointer value( if U can call so) cannot be changed . Only the contents of that memory can be changed which is reflected in the called function.

    Thus when we want to copy a big block from some other memory to the one we need , rather than copying that full block having passed the parameter as Reference . if we pass the paramter as a pointer just changing the value of that pointer should help but then remember now 2 pointers point to same memory location .


    Yash.


  8. #8
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: References versus pointers

    Thanks Yash,

    Brendan


  9. #9
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: References versus pointers

    Thanks Michael,

    Brendan


  10. #10
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: References versus pointers

    Hi Paul,

    The language defines copy constructors (and assignment operators) to use references - but that's not to say that they're the best things to use :-)

    Thanks,

    Brendan


  11. #11
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: References versus pointers

    Thanks Matt,

    Brendan


  12. #12
    Guest

    Re: References versus pointers

    Your question would be better if reversed. Why would you ever want to use a pointer instead of a reference?

    Off the top of my head I can think of only a few...

    1. You want to be able to pass NULL (can't be done with a reference)
    2. You'll change the pointer inside the function. (say your passing a pointer to a buffer that you'll be scanning for some value and your using the passed pointer to scan with)


    But if your not doing one of the above.. you should always use a reference.


  13. #13
    Guest

    Re: References versus pointers

    You need not be any more concerned about passing the correct data type with a reference than with a pointer because in both cases the compiler won't let you pass the incorrect type. The snippet of code you posted, for example, won't compile.

    Maybe you where trying to point out is the problem with code like...

    class Foo
    {
    SomeFunction(int val);
    SomeFunction(long* pLong);
    };

    The compiler will convert the long* into an int and call SomeFunction(int) instead of SomeFunction(long*). The same would probably be true for a long& but I didn't take the time to test it.









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