DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007

    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?
        TempClass() << "Stuff"; // here?
    } // here?

  2. #2
    Join Date
    Dec 2003
    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.

  3. #3
    Join Date
    Nov 2003
        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

  1. Replies: 3
    Last Post: 08-20-2008, 07:57 AM
  2. Replies: 1
    Last Post: 02-29-2008, 08:42 AM
  3. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 05:50 PM
  4. HtmlSelectElement Object
    By JD in forum Web
    Replies: 0
    Last Post: 05-01-2001, 02:46 PM
  5. Object Modeling for the WEB
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

By using this site, you agree to the Privacy Policy