memory and passing pointers

Discussion in 'C++' started by Ivan Liu, Sep 29, 2006.

  1. Ivan Liu

    Ivan Liu Guest

    Hi,

    I'd like to ask if passing an object as an pointer into a function
    evokes the copy constructor.

    Ivan
     
    Ivan Liu, Sep 29, 2006
    #1
    1. Advertising

  2. Ivan Liu

    Ron Natalie Guest

    Ivan Liu wrote:

    > I'd like to ask if passing an object as an pointer into a function
    > evokes the copy constructor.


    No. Copying pointers has no bearing on the object that they point to.
     
    Ron Natalie, Sep 29, 2006
    #2
    1. Advertising

  3. Ivan Liu

    Ivan Liu Guest

    Thanks for the reply.

    So the efficiency is as good as passing a reference? If so why pass
    reference?


    Ron Natalie wrote:
    > Ivan Liu wrote:
    >
    > > I'd like to ask if passing an object as an pointer into a function
    > > evokes the copy constructor.

    >
    > No. Copying pointers has no bearing on the object that they point to.
     
    Ivan Liu, Sep 29, 2006
    #3
  4. Ivan Liu

    Ron Natalie Guest

    Ivan Liu wrote:
    > Thanks for the reply.
    >
    > So the efficiency is as good as passing a reference? If so why pass
    > reference?
    >
    >

    You are asking extremely vague questions. The main reasons references
    exist are to accomodate overloading. If it were not for this C++
    could have gotten by without them.

    As for stylistically, it would depend on the situation. Nothing
    is gained by switching from pointers to references blindly.
     
    Ron Natalie, Sep 29, 2006
    #4
  5. Ivan Liu

    benben Guest

    >>> I'd like to ask if passing an object as an pointer into a function
    >>> evokes the copy constructor.

    >> No. Copying pointers has no bearing on the object that they point
    >> to.

    >
    >
    > So the efficiency is as good as passing a reference? If so why pass
    > reference?
    >


    Readability I'd say, although it is disputable. Some people prefer
    passing pointers and some prefer passing reference. It's up to you to
    chose your own style.

    One place you really want to pass references instead of pointers is
    overloaded operators.

    Regards,
    Ben
     
    benben, Sep 29, 2006
    #5
  6. Ivan Liu

    Jim Langston Guest

    "Ivan Liu" <> wrote in message
    news:...
    > Ron Natalie wrote:
    >> Ivan Liu wrote:
    >>
    >> > I'd like to ask if passing an object as an pointer into a function
    >> > evokes the copy constructor.

    >>
    >> No. Copying pointers has no bearing on the object that they point to.

    > Thanks for the reply.
    >
    > So the efficiency is as good as passing a reference? If so why pass
    > reference?


    Please don't top post in this newsgroup. Message rearranged.

    The main reason I pass references into functions is so I can use . instead
    of ->
     
    Jim Langston, Sep 29, 2006
    #6
  7. Ivan Liu

    Gavin Deane Guest

    Please don't top-post. Thank you. Rearranged.

    Ivan Liu wrote:
    > Ron Natalie wrote:
    > > Ivan Liu wrote:
    > >
    > > > I'd like to ask if passing an object as an pointer into a function
    > > > evokes the copy constructor.

    > >
    > > No. Copying pointers has no bearing on the object that they point to.

    > Thanks for the reply.
    >
    > So the efficiency is as good as passing a reference? If so why pass
    > reference?


    There is a fundamental difference between passing references to objects
    and passing pointers to objects. A reference must always refer to a
    valid object. A pointer may refer to a valid object or may be null.
    Therefore, if a function accepts a pointer, it must accept the
    possibility of a null pointer and check the pointer value before using
    it. Change the function to accept a reference and this extra
    responsibility is removed. The function can safely assume that an
    object is there.

    The only time this is not an advantage is, of course, when, in your
    design, the possibility of an object not existing makes sense. A
    reference is no use here because it always referes to a valid object.
    Pass a pointer and the function can query that pointer to see if it is
    null or not.

    A reference says: Here is the object for you to work with.
    A pointer says: Here is an indicator that tells you whether the
    optional object exists and, if so, where it is.

    Summary: use references when you can and pointers when you must.

    Gavin Deane
     
    Gavin Deane, Sep 29, 2006
    #7
  8. benben wrote:
    > >>> I'd like to ask if passing an object as an pointer into a function
    > >>> evokes the copy constructor.
    > >> No. Copying pointers has no bearing on the object that they point
    > >> to.

    > >
    >>
    >> So the efficiency is as good as passing a reference? If so why pass
    >> reference?
    >>

    >
    > Readability I'd say, although it is disputable. Some people prefer
    > passing pointers and some prefer passing reference. It's up to you to
    > chose your own style.
    >
    > One place you really want to pass references instead of pointers is
    > overloaded operators.



    Two things about references that are not supported by pointers.

    a) References can't be changed after being initialized
    b) The lifetime of a reference can be transferred to objects.

    (from an earlier post of mine)

    #include <iostream>
    #include <ostream>

    struct A
    {
    A()
    {
    std::cout << "A default\n";
    }

    A( const A & )
    {
    std::cout << "A copy\n";
    }

    A & operator= ( const A & )
    {
    std::cout << "A operator =\n";
    return * this;
    }

    ~A()
    {
    std::cout << "A Destruct\n";
    }

    };

    int main()
    {
    {
    const A & a = A();

    std::cout << "Temporary lives !\n";

    A y( a );
    }

    std::cout << "Temporary is gone !\n";

    {
    const A * a = & A();

    std::cout << "Temporary is gone already - a dangles!\n";
    }

    }
     
    Gianni Mariani, Sep 29, 2006
    #8
  9. Ivan Liu

    Ron Natalie Guest

    Gavin Deane wrote:
    > A pointer may refer to a valid object or may be null.
    > Therefore, if a function accepts a pointer, it must accept the
    > possibility of a null pointer and check the pointer value before using
    > it.


    That's certainly not true. The C++ standard library itself is
    full of functions that assume (they are not required to check)
    that the caller is not passing a NULL or otherwise invalid pointer.


    >
     
    Ron Natalie, Sep 29, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    449
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    1
    Views:
    656
    -berlin.de
    Mar 28, 2005
  3. Marc Thrun
    Replies:
    15
    Views:
    865
    Tim Rentsch
    Oct 4, 2005
  4. cham
    Replies:
    5
    Views:
    773
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    688
Loading...

Share This Page