Best way to return list of objects in C++?

前端 未结 5 728
故里飘歌
故里飘歌 2021-02-12 08:48

It\'s been a while since I programmed in C++, and after coming from python, I feel soooo in a straight jacket, ok I\'m not gonna rant.

I have a couple of functions that

5条回答
  •  终归单人心
    2021-02-12 09:23

    The only thing I can see is that your forcing a copy of the list you return. It would be more efficient to do something like:

      void DoSomething(const std::vector& in, std::vector& out)
      {
      ...
      // no need to return anything, just modify out
      }
    

    Because you pass in the list you want to return, you avoid the extra copy.

    Edit: This is an old reply. If you can use a modern C++ compiler with move semantics, you don't need to worry about this. Of course, this answer still applies if the object you are returning DOES NOT have move semantics.

提交回复
热议问题