Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > which comes first?

Reply
Thread Tools

which comes first?

 
 
Jason Sachs
Guest
Posts: n/a
 
      02-15-2007
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-15-2007
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


 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      02-16-2007
On 15 Feb 2007 06:41:51 -0800, "Jason Sachs" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to identify which interface a broadcast packet comes in on? Lincoln Yeoh Python 1 09-03-2005 04:15 PM
XSL template priorities: Which comes first, priority or predicates? Soren Kuula XML 2 04-13-2005 10:11 PM
Re: FW: Check out critical pack which comes from the Microsoft Netuser 58 Computer Information 0 11-11-2003 02:12 AM
Check out the corrective patch which comes from the Microsoft Gary Tsang Microsoft Certification 2 09-21-2003 01:35 AM
Check out the corrective patch which comes from the Microsoft Gary Tsang MCSE 2 09-21-2003 01:35 AM



Advertisments