returning an object by value using a non const copy constructorargument

Discussion in 'C++' started by Ralph, Jun 11, 2008.

  1. Ralph

    Ralph Guest

    Hi,

    I was wondering how to get the following working. I know std::auto_ptr
    does something similar but how ?


    Thanks,

    Ralph.


    class Test
    {
    public:
    Test() : m_b(true) {};

    // copy constructor which resets the object beeing copied
    Test(Test &t) : m_b(t.m_b) { t.m_b = false; };

    bool get() const { return m_b; };
    private:
    bool m_b;
    };

    Test f()
    {
    // this will not compile...
    return Test();
    }
     
    Ralph, Jun 11, 2008
    #1
    1. Advertising

  2. Ralph

    Fran Guest

    On Jun 11, 3:07 pm, Ralph wrote:

    > class Test
    > {
    > public:
    > Test() : m_b(true) {};
    >
    > // copy constructor which resets the object being copied
    > Test(Test &t) : m_b(t.m_b) { t.m_b = false; };


    Why would you make your _copy_ constructor produce an object that is
    not, in fact, a copy of the original object?

    > bool get() const { return m_b; };
    > private:
    > bool m_b;
    > };
    >
    > Test f()
    > {
    > // this will not compile...
    > return Test();
    > }


    Try this instead:

    Test f()
    {
    Test x;
    return x;
    }
     
    Fran, Jun 11, 2008
    #2
    1. Advertising

  3. Ralph

    Guest

    On Jun 11, 12:07 pm, Ralph <> wrote:
    > Hi,
    >
    > I was wondering how to get the following working. I know std::auto_ptr
    > does something similar but how ?


    You may have already looked at your implementation of auto_ptr but it
    uses an auxiliary class: auto_ptr_ref.

    > Test f()
    > {
    > // this will not compile...
    > return Test();
    >
    > }


    Ali
     
    , Jun 11, 2008
    #3
  4. Ralph

    Ralph Guest

    On Wed, 11 Jun 2008 12:15:07 -0700, Fran wrote:

    > On Jun 11, 3:07 pm, Ralph wrote:
    >
    >> class Test
    >> {
    >> public:
    >> Test() : m_b(true) {};
    >>
    >> // copy constructor which resets the object being copied
    >> Test(Test &t) : m_b(t.m_b) { t.m_b = false; };

    >
    > Why would you make your _copy_ constructor produce an object that is
    > not, in fact, a copy of the original object?


    Well std::auto_ptr can do it and I was just curious how it works.


    > Test f()
    > {
    > Test x;
    > return x;
    > }


    Maybe, but the other should be possible, at least std::auto_ptr does it.
     
    Ralph, Jun 12, 2008
    #4
  5. Ralph

    Ralph Guest

    On Wed, 11 Jun 2008 13:59:51 -0700, acehreli wrote:

    >> I was wondering how to get the following working. I know std::auto_ptr
    >> does something similar but how ?

    >
    > You may have already looked at your implementation of auto_ptr but it
    > uses an auxiliary class: auto_ptr_ref.


    I looked at an implementation but it did not made much sense to me,
    however I found another one which actually has comments about the
    auto_ptr_ref stuff and it makes more sense now.

    I found it here:
    http://gcc.gnu.org/onlinedocs/libstdc /latest-doxygen/a01857.html

    Ralph.
     
    Ralph, Jun 12, 2008
    #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. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  2. fungus
    Replies:
    13
    Views:
    891
    fungus
    Oct 31, 2008
  3. Francesco
    Replies:
    0
    Views:
    405
    Francesco
    Aug 31, 2009
  4. fdm
    Replies:
    11
    Views:
    795
    Francesco
    Sep 1, 2009
  5. Jens Thoms Toerring

    Const/non-const pointer returning method

    Jens Thoms Toerring, May 25, 2010, in forum: C++
    Replies:
    11
    Views:
    722
    John H.
    May 26, 2010
Loading...

Share This Page