function return design

This is a discussion on function return design within the C++ Programming forums, part of the General Programming Boards category; Hello I'm calling a function from some class which is a member of another class. This function parses some data ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    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

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21