How to “return an object” in C++?

后端 未结 8 548
礼貌的吻别
礼貌的吻别 2020-11-22 09:25

I know the title sounds familiar as there are many similar questions, but I\'m asking for a different aspect of the problem (I know the difference between having things on t

8条回答
  •  情话喂你
    2020-11-22 09:44

    Did you try to use smart pointers (if Thing is really big and heavy object), like auto_ptr:

    
    std::auto_ptr calculateThing()
    {
      std::auto_ptr thing(new Thing);
      // .. some calculations
      return thing;
    }
    
    
    // ...
    {
      std::auto_ptr thing = calculateThing();
      // working with thing
    
      // auto_ptr frees thing 
    }
    

提交回复
热议问题