Re: can someone explain to me, a C/Hw guy, what the diff is between a reference (&) and a pointer?

From: steve (_no_at_way.com)
Date: 09/16/04


Date: Thu, 16 Sep 2004 15:42:37 +0200

Added to what everyone else has said, it is also important to understand the
implication of ownership when deciding whether to use a reference or a
pointer.

Pointer are generally used to take ownership ( responsible for deleting it
if it was new'd ) of an object, whereas a reference would be use when
someone else has ownership of the object.

While you could implement the "ownership/non-ownership" using a pointer in
both cases, it does make the code self documenting, and lets the compiler
check for bugs :-)

HTH
-Steve

"bo" <bo@cephus.com> wrote in message
news:9q1jk01jqfp8rnri88udpdcmfqvp8clpgu@4ax.com...
> And why and where one should use one vs. the other?
>
> Verbally, it seems like semantics to me--but obviously there is some
> actual difference that makes references different and or preferable
> over pointers in some cases...
>
> TIA
>



Relevant Pages

  • Re: The future of Python immutability
    ... This is simple until "ownership" needs to be transferred. ... when a reference to an object is created, ... It takes two reference counts and a pointer ... tree root is synchronized. ...
    (comp.lang.python)
  • Re: Two questions about using shared_ptr
    ... Otherwise I'd just take raw pointer. ... the function wants to store the pointer somewhere to be used after it returns, thus sharing ownership with the caller. ... Of course the caller is free to drop its reference right after the function call, thus effectively transferring ownership to whatever entity the called function works on behalf of. ... question are passed around per smart pointers, ...
    (microsoft.public.vc.language)
  • Re: STL Vector - pass by reference?
    ... When you pass a pointer, the "implication" is that it could be NULL, and ... that the function will take some special action in this case. ... a reference, it cannot be NULL (every reference must be initialized to ... pointer can be converted to a reference. ...
    (microsoft.public.vc.language)
  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)