-
Mind block: temp object destruction
I've got a case where I want a class to be used only as a temporary object which completes the action at destruction. (For "complete" think "transmits itself").
So, when is a temporary object destroyed?
Code:
{
TempClass() << "Stuff"; // here?
...
} // here?
-
I don't know for sure, but you can find out by putting prints (with flushes of course!) into your class destructor and again a various places after the temporary was created to nail it down. I also do not know if the compiler and optimizations can change this much or it it is set in stone --- hopefully the compiler has some leeway as its clearly faster to destroy once and create once if such code were called in a loop, hanging onto the temporary inside the loop to reuse for example, while other times it may be better to destory immediately. It may also vary a little by calling conventions, not sure. Or the standard may have a set way to do it that I am unaware of.
I would not do this. Poke it into the temporary, call a class function (even if you make the temporary a functor), and let the destructor be normal (processing data in the destructor is not really normal). You pay for an extra function call, but given that you are willing to use a temporary and suffer a full object creation + destruction, thats peanuts.
-
Code:
{
TempClass() << "Stuff"; // here!
...
}
The general rule is this: unless the temp is bound to a reference, which isn't the case here, it's destroyed immediately upon reaching the ; that terminates its declaration. If it were destroyed upon exiting the block, i.e., at }, it would be a normal object, not a temp.
Danny Kalev
Similar Threads
-
By chayolle in forum VB Classic
Replies: 3
Last Post: 08-20-2008, 07:57 AM
-
By JohnWong in forum .NET
Replies: 1
Last Post: 02-29-2008, 08:42 AM
-
By Jaco de Villiers in forum XML
Replies: 1
Last Post: 06-01-2001, 05:50 PM
-
Replies: 0
Last Post: 05-01-2001, 02:46 PM
-
By Greg Dirst in forum authorevents.appleman
Replies: 1
Last Post: 04-10-2000, 02:56 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks