-
function return design
Hello
I'm calling a function from some class which is a member of another class.
This function parses some data and sets 3 strings (objects).
Now I wonder what is the best way to pass back data to the place where this function is being called? It would be cool to be able to return 3 values but unfortunately this isnt possible so the other option is to make a struct/class with there 3 members and return it.
Is there any better option?
What do you think is the best approach for solving this problem?
Maybe using boost::tuple which is basicaly a multi-pair?
Thanks for help
-
See how getline passes information back to the caller by receiving an argument by reference: http://www.cppreference.com/cppstring/getline.
That seems more convenient (to me) than receiving the information in a tuple and having to pull the tuple apart. However, it depends on exactly what you're doing. One question is, how are your three strings related to one another? Will the user want to keep this three-string object in a condensed state? Maybe then you'd make a new datatype for just that purpose. There's some limit to the number of arguments you can pass by reference to receive return values before it starts getting cumbersome, but it's not definite or absolute, so just ask what will be most convenient for the caller to use.
If you're setting 3 strings and if your object is storing the strings internally, maybe you will want to return nothing, allowing the caller to request the information later, if it's needed.
Edit: Generally, I'd lean towards returning a tuple, just out of the general principle of avoiding mutable variables whenever possible. But if your class is already doing a state modification, I guess you don't care about that.