Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With C++0x/C++11 you can use this:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;tuple&gt; using namespace std; tuple&lt;int, unsigned, string, int&gt; getValues() { return tuple&lt;int, unsigned, string, int&gt;(1, 2, "3", 4); } int main() { int a; unsigned b; string c; int d; tie(a, b, c, d) = getValues(); cout &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", " &lt;&lt; d &lt;&lt; endl; return 0; } </code></pre> <p>Compile it with</p> <pre><code>g++ tuple_test.cpp -std=c++0x -o tuple_test </code></pre> <p>And and if you run the programm it will output this:</p> <pre><code>1, 2, 3, 4 </code></pre> <p>But it's better to use references like this (i would say):</p> <pre><code>void getValues(int&amp; a, unsigned&amp; b, string&amp; c, int&amp; d) { a = 1; b = 2; c = "3"; d = 4; } int main() { ... getValues(a, b, c, d) ... } </code></pre> <p>Uch thats what I get for not reading the question carefully...</p> <blockquote> <blockquote> <p>Can a function only return one value, unless it is a pointer to an array?</p> </blockquote> </blockquote> <p>Yeah you only can return 1 single value, but this single value can include multiply values (struct, class, array).</p> <blockquote> <blockquote> <p>You can return multiple data items using a structure but using pointer (I don't understand this) will use memory more efficiently. Is this correct?</p> </blockquote> </blockquote> <p>True. But when you use pointers it depends on how you use it. When you dynamic allocate it each function call it wont be very efficient and you would need to deallocate the memory manually after usage. When you use a global-array/struct it will be efficient. But can give you problems when you call the function multiply times.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload