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

前端 未结 6 2093
眼角桃花
眼角桃花 2021-02-12 08:56

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

6条回答
  •  清酒与你
    2021-02-12 09:29

    If you want to be really hardcore, you could use boost::tuple.

    tuple add_multiply_divide(int a, int b) {
      return make_tuple(a+b, a*b, double(a)/double(b));
    }
    

    But since it seems all your objects are of a single, non-polymorphic type, then the std::vector is all well and fine. If your types were polymorphic (inherited classes of a base class) then you'd need a vector of pointers, and you'd need to remember to delete all the allocated objects before throwing away your vector.

提交回复
热议问题