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

后端 未结 8 715
礼貌的吻别
礼貌的吻别 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:32

    Just create the object and return it

    Thing calculateThing() {
        Thing thing;
        // do calculations and modify thing
         return thing;
    }
    

    I think you'll do yourself a favor if you forget about optimization and just write readable code (you'll need to run a profiler later - but don't pre-optimize).

提交回复
热议问题