Returning multiple values from a C++ function

后端 未结 21 2520
别跟我提以往
别跟我提以往 2020-11-22 01:04

Is there a preferred way to return multiple values from a C++ function? For example, imagine a function that divides two integers and returns both the quotient and the rema

21条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 01:45

    It's entirely dependent upon the actual function and the meaning of the multiple values, and their sizes:

    • If they're related as in your fraction example, then I'd go with a struct or class instance.
    • If they're not really related and can't be grouped into a class/struct then perhaps you should refactor your method into two.
    • Depending upon the in-memory size of the values you're returning, you may want to return a pointer to a class instance or struct, or use reference parameters.

提交回复
热议问题