Thread: returning a struct/class, and destructors

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    returning a struct/class, and destructors

    If a function returns a class (not as a reference/pointer), are destructors called for the stuff on the stack?

    Code:
    class Bwah{
       public:
          int x;
          Bwah():
             x(0) {}
             
          ~Bwah(){
             cout << "x : " << x << endl;
          }
    };
    
    class Assigner{
       public:
          static Bwah boo(){
             Bwah bwah;
             return bwah;
          }
    };
    
    int main () {
    
       {
          Bwah boo = Assigner::boo();
          
          cout << "got here.." << endl;
          
       }
       
    }
    In that example, the destructor only gets called once, after "got here". Where can I read more about this stuff?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are observing the effects of return value optimisation, where the copy construction is elided in order to avoid what would normally be unnecessary copying.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The short answer is yes.

    The reason you are seeing only one object being destroyed is that only one object is being created.

    The C++ standard has a rule that explicitly allows a compiler to avoid creating/destroying any temporary object, if the only way to detect existence of such an object is by tracking constructor and destructor calls. Your compiler is presumably taking advantage of that rule (as it allows performance optimisations). Look up topics like "return value optimisation".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Thanks, understood

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructors error
    By DarrenY in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2007, 04:22 AM
  2. Destructors in STL?
    By sawer in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2006, 09:35 AM
  3. deques, vectors and destructors.
    By Hunter2 in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2005, 06:29 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM