which comes first?

J

Jason Sachs

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;
}
 
V

Victor Bazarov

Jason said:
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
 
D

Dave Rahardja

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top