Can I pass auto_ptr in this way?

Discussion in 'C++' started by zl2k, Sep 7, 2010.

  1. zl2k

    zl2k Guest

    hi, there,

    Can I pass auto_ptr in the following manner?

    auto_ptr<T> obj; // The obj is not initialized and null here, right?
    function(obj);
    // Do I get the initialized obj here?

    void function(auto_ptr<T> obj){// can I pass a null auto_ptr into the
    function and get initialized inside?
    obj(new T());
    }

    Thanks a lot.

    zl2k
    zl2k, Sep 7, 2010
    #1
    1. Advertising

  2. * zl2k, on 07.09.2010 05:08:
    > hi, there,
    >
    > Can I pass auto_ptr in the following manner?
    >
    > auto_ptr<T> obj; // The obj is not initialized and null here, right?
    > function(obj);
    > // Do I get the initialized obj here?
    >
    > void function(auto_ptr<T> obj){// can I pass a null auto_ptr into the
    > function and get initialized inside?
    > obj(new T());
    > }


    No, for two reasons.

    First, regardless of the functionality of std::auto_ptr, when you pass an object
    O by value, like foo(O) where foo's formal argument is by value, then the effect
    is a copy construction of the formal argument from the actual argument. This
    means that foo can't do anything directly with the original actual argument.

    Secondly, the syntax

    obj( new T() );

    as a statement is a function call, and when obj is of class type it means

    obj.operator()( new T() )

    but std::auto_ptr has no operator(), no function call operator.

    What you can do is something like

    auto_ptr<T> foo() { return auto_ptr<T>( new T() ); }

    //...
    auto_ptr<T> o1( foo() );
    auto_ptr<T> o2 = foo();


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 7, 2010
    #2
    1. Advertising

  3. zl2k

    Goran Pusic Guest

    You seem to be looking for

    void foo(auto_ptr<T>& p) { p=auto_ptr<T>( new T() ); }

    (You also seem to not understand a pass-by-reference, and what are
    consequences when you pass auto_ptr by reference; and due to the
    nature of auto_ptr, there are additional things to understand when you
    pass auto_ptr by ref).

    Goran.
    Goran Pusic, Sep 7, 2010
    #3
  4. * Goran Pusic, on 07.09.2010 08:46:
    > You seem to be looking for
    >
    > void foo(auto_ptr<T>& p) { p=auto_ptr<T>( new T() ); }
    >
    > (You also seem to not understand a pass-by-reference, and what are
    > consequences when you pass auto_ptr by reference; and due to the
    > nature of auto_ptr, there are additional things to understand when you
    > pass auto_ptr by ref).


    Hi Goran.

    Please quote sufficiently from the message that you're responding to, to
    establish context.

    Also, please post your response to a message M as a follow-up to M.


    Thanks in advance,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 7, 2010
    #4
  5. zl2k

    DDD Guest

    On 7 sep, 14:52, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * Goran Pusic, on 07.09.2010 08:46:
    >
    > > You seem to be looking for

    >
    > > void foo(auto_ptr<T>&  p) { p=auto_ptr<T>( new T() ); }

    >
    > > (You also seem to not understand a pass-by-reference, and what are
    > > consequences when you pass auto_ptr by reference; and due to the
    > > nature of auto_ptr, there are additional things to understand when you
    > > pass auto_ptr by ref).

    >
    > Hi Goran.
    >
    > Please quote sufficiently from the message that you're responding to, to
    > establish context.
    >
    > Also, please post your response to a message M as a follow-up to M.
    >
    > Thanks in advance,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>


    Maybe just need to know auto_ptr<T> obj is a class obj. It's a auto
    variable.
    DDD, Sep 7, 2010
    #5
    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. bucher
    Replies:
    1
    Views:
    983
    Ron Natalie
    Oct 22, 2003
  2. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,552
    Dave Rahardja
    Jan 11, 2005
  3. Ami
    Replies:
    9
    Views:
    752
    James Kanze
    Dec 8, 2008
  4. Sousuke
    Replies:
    9
    Views:
    1,146
    Bart van Ingen Schenau
    Mar 16, 2010
  5. H
    Replies:
    2
    Views:
    97
Loading...

Share This Page