which comes first?

Discussion in 'C++' started by Jason Sachs, Feb 15, 2007.

  1. Jason Sachs

    Jason Sachs Guest

    In the function copy_info below, which happens first:
    (a) X gets copied
    (b) Y's destructor is called

    class T
    {
    X m_info;
    L m_semaphore;

    public:
    X copy_info();
    }

    X T::copy_info()
    {
    Y ylock(m_semaphore);

    return m_info;
    }

    and if Y's destructor happens first, is there any way to reverse the
    order besides a brute force extra copy:
    X T::copy_info()
    {
    Y ylock(m_semaphore);

    X stupid_temporary(m_info);
    return stupid_temporary;
    }
    Jason Sachs, Feb 15, 2007
    #1
    1. Advertising

  2. Jason Sachs wrote:
    > In the function copy_info below, which happens first:
    > (a) X gets copied
    > (b) Y's destructor is called
    >
    > class T
    > {
    > X m_info;
    > L m_semaphore;
    >
    > public:
    > X copy_info();
    > }
    >
    > X T::copy_info()
    > {
    > Y ylock(m_semaphore);
    >
    > return m_info;
    > }


    From what I can gather, creation of the temporary occurs as part of
    executing the 'return' statement, and the destruction of 'ylock'
    happens upon "exiting the scope", which is the next line after that,
    which suggests that the temporary is created before 'ylock' is
    destroyed.

    >
    > and if Y's destructor happens first, is there any way to reverse the
    > order besides a brute force extra copy:
    > X T::copy_info()
    > {
    > Y ylock(m_semaphore);
    >
    > X stupid_temporary(m_info);
    > return stupid_temporary;
    > }


    'stupid_temporary' local object will be copied into a temporary
    first (as part of the 'return' statement), then it will be destroyed.
    Then 'ylock' will be destroyed.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 15, 2007
    #2
    1. Advertising

  3. On 15 Feb 2007 06:41:51 -0800, "Jason Sachs" <> wrote:

    >In the function copy_info below, which happens first:
    >(a) X gets copied
    >(b) Y's destructor is called
    >
    >class T
    >{
    > X m_info;
    > L m_semaphore;
    >
    >public:
    > X copy_info();
    >}
    >
    >X T::copy_info()
    >{
    > Y ylock(m_semaphore);
    >
    > return m_info;
    >}
    >
    >and if Y's destructor happens first, is there any way to reverse the
    >order besides a brute force extra copy:
    >X T::copy_info()
    >{
    > Y ylock(m_semaphore);
    >
    > X stupid_temporary(m_info);
    > return stupid_temporary;
    >}


    X gets copied, then Y's destructor is called.

    -dr
    Dave Rahardja, Feb 16, 2007
    #3
    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. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    !!web control not recognised1 what comes first?

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    464
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Nov 29, 2004
  2. Soren Kuula
    Replies:
    2
    Views:
    562
    Malcolm Dew-Jones
    Apr 13, 2005
  3. Jack Dowson

    Chick and egg,which comes first?

    Jack Dowson, Sep 16, 2007, in forum: Java
    Replies:
    18
    Views:
    581
  4. Nathan Sokalski
    Replies:
    4
    Views:
    578
    PJ on Development
    Nov 8, 2007
  5. Nathan Sokalski
    Replies:
    4
    Views:
    191
    PJ on Development
    Nov 8, 2007
Loading...

Share This Page