returning a struct/class, and destructors

This is a discussion on returning a struct/class, and destructors within the C++ Programming forums, part of the General Programming Boards category; If a function returns a class (not as a reference/pointer), are destructors called for the stuff on the stack? Code: ...

  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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    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%.

  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, 05:29 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21