Returning Large Objects in Functions

后端 未结 9 1242
無奈伤痛
無奈伤痛 2020-11-27 14:17

Compare the following two pieces of code, the first using a reference to a large object, and the second has the large object as the return value. The emphasis on a \"large o

9条回答
  •  悲哀的现实
    2020-11-27 14:38

    A somewhat idiomatic solution would be:

    std::auto_ptr getObjData()
    {
      std::auto_ptr a(new LargeObj);
      a->fillWithData();
      return a;
    }
    
    int main()
    {
      std::auto_ptr a(getObjData());
    }
    

提交回复
热议问题