How do I return hundreds of values from a C++ function?

前端 未结 9 1426
无人共我
无人共我 2021-01-01 04:37

In C++, whenever a function creates many (hundreds or thousands of) values, I used to have the caller pass an array that my function then fills with the output values:

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 05:15

    If you ever have a group of objects, chances are you have at least a few methods that work on that group of objects (otherwise, what are you doing with them?)

    If that's the case, it would make sense to have those methods in a class that contain both said objects and methods.

    If that makes sense and you have such a class, return it.

    I virtually never find myself thinking that I wish I could return more than one value. By the fact that a method should only do one small thing, your parameters and return values tend to have a relationship, and so are more often than not deserving of a class that contains them, so returning more than one value is rarely interesting (Maybe I wished for it 5 times in 20 years--each time I refactored instead, came up with a better result and realized my first attempt was sub-standard.)

提交回复
热议问题