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

    to pass an object by reference or to pass a pointer to the object

    Hi,

    I am having difficulty choosing between whether to pass an object by reference or to pass a pointer to it to a function. This is a generic question, not related to any particular class. Basically, if you want a function ro receive the address of an object, which option is preferable ?

    I'm aware of a few considerations :

    1 A pointer can have a null value. If the function declares a pointer parameter, the caller can pass in a NULL, and the function can test for it; with a reference parameter, the caller must specify an object.

    2 If the function returns a pointer, it can return NULL, and the caller can test for it, with a reference return, the function must return an object.

    3 One advantage of using a reference is that you won't be passed a NULL pointer. And so there’s no need to test for one.

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

    Regards,

    Brendan





  2. #2
    Join Date
    Apr 1999
    Posts
    32

    Re: to pass an object by reference or to pass a pointer to the object

    In reply to:


    Basically, if you want a function ro receive the address of an object, which option is preferable ?





    If you want a function to receive the address of an object, you have only one option. You must use a pointer. If you use a reference you don't pass the address of the obejct, you pass the object itself in effect.

    In reply to:


    2 If the function returns a pointer, it can return NULL, and the caller can test for it, with a reference return, the function must return an object.





    And alternative approach is to throw an exception.
    In reply to:


    3 One advantage of using a reference is that you won't be passed a NULL pointer. And so there’s no need to test for one.




    No, but you could be passed an object that is not correctly initialised. For example, some objects require that you call .Create() on them before they are ready for use. It's possible that the following situation occurs:


    CMyObj myObj;

    // myobj.Create(); // not called.
    func (myobj);





    The myobj object is not ready for use because the Create() was commented out. What I am saying is that you need to be able to check the validity of an object as much as you do a pointer you are passed.


    In reply to:


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




    Why would you need to dereference the pointer? What can you do with dot notation that you can't with pointer notation? Unless you are in the habit of code like...

    int func(CMyObj *pObj)
    {
    ...
    (*(pObj)).iVal = 1;
    (*(pObj)).Update();
    ...
    }





    instead of...


    int func(CMyObj *pObj)
    {
    ...
    pObj->iVal = 1;
    pObj->Update();
    ...
    }





    ...I don't understand. The only time I can think of that you would need to dereference the pointer is when you need to call a function that expects an object (rather than a pointer to one) or when the object is something like an array and you'd rather use [] notation rather than + sizeof().

    Maybe I'm missing something. =)

    Personally, I'd use references. The access syntax looks simpler (. as with pass-by-value), and they don't have the copy overhead of pass by value.


    --
    Daren Chandisingh

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

    Re: to pass an object by reference or to pass a pointer to the object

    Hi Daren,

    You said : "If you want a function to receive the address of an object, you have only one option. You must use a pointer. If you use a reference you don't pass the address of the
    obejct, you pass the object itself in effect."

    I disagree. Yes- the "access syntax" allows you to use the name of the object as if you were using the object itself. But it is not the object you have passed. It is its address.

    Brendan


  4. #4
    Join Date
    Apr 1999
    Posts
    32

    Re: to pass an object by reference or to pass a pointer to the object

    You'll notice that I said "in effect." Yes, I know that the address is passed, but I also know that the access syntax is as pass-by-value. However, you get this without the overhead of copy constructors and the like, or with bloated stack gubbins, so in effect you've passed the object itself.

    Well, I know what I mean, FFS! =)


    --
    Daren Chandisingh

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

    Re: to pass an object by reference or to pass a pointer to the object

    Hi Daren,

    I'm wondering if your FFS acronym is what I think it is :-) That'd be a first !

    I think we're on a wavelength.

    Thanks for the replies and enjoy the weekend,

    Brendan


  6. #6
    Join Date
    Apr 1999
    Posts
    32

    Re: to pass an object by reference or to pass a pointer to the object

    I'm wondering if your FFS acronym is what I think it is :-)

    I suspect that it is.

    Ah, the weekend. Two days of CTF, CTF and more CTF. And a little Q3ATest for good measure. A welcome break from the daily codegrind. wh00p! =)


    --
    Daren Chandisingh

  7. #7
    Join Date
    May 1999
    Location
    PA
    Posts
    38

    Re: to pass an object by reference or to pass a pointer to the object

    Hi,
    According to me any one can use Pointer or reference if
    1) There is a possiblilty or required that Called function will modify the object passed to it.

    We have to use reference in the following case.
    1) If the caller function wants to retain the ownership of the object even if the function it called to which that object is passed is returned. In this case reference will make sure that the ownership is not transfered any where else.
    2) We may use reference where the assumption is that the object is already created before the call to the function to which we have to pass this object.

    We can use the pointer in case of following cases.
    1) if the the function to which the pointer to an object is passed, is supposed to allocate or initialize memory for this pointer
    2) Or it is the possibility that some time we need to pass NULL instead of the pointer to an object.

    I hope this answer will help.
    Thanks
    Chetan

    Practice makes the man perfect.

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