Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use references for <code>a</code> and <code>b</code>.</p> <pre><code>void getvals(int &amp;a, int &amp;b) { cout &lt;&lt; "input value a "; cin &gt;&gt; a; cout &lt;&lt; "input value b "; cin &gt;&gt; b; } </code></pre> <p>This declares <code>getvals()</code> to take two reference parameters. Modification to the reference of an object modifies the object that was passed in to the function call.</p> <p>Without the reference, the parameter is passed by value, which creates a copy of the object passed to the function. Then, modifications made to the parameter in the function only affect the copy.</p> <p>Alternatively, you can use <code>std::pair&lt;int, int&gt;</code> to return two integer values from your function (it won't need <em>out-parameters</em> then). You can manually unpack the <code>first</code> and <code>second</code> members into your variables <code>x</code> and <code>y</code>, or you can implement a helper class to do that for you. For example:</p> <pre><code>std::pair&lt;int, int&gt; getvals () { std::pair&lt;int, int&gt; p; std::cin &gt;&gt; p.first; std::cin &gt;&gt; p.second; return p; } template &lt;typename T, typename U&gt; struct std_pair_receiver { T &amp;first; U &amp;second; std_pair_receiver (T &amp;a, U &amp;b) : first(a), second(b) {} std::pair&lt;T, U&gt; operator = (std::pair&lt;T, U&gt; p) { first = p.first; second = p.second; return p; } }; template &lt;typename T, typename U&gt; std_pair_receiver&lt;T, U&gt; receive_pair (T &amp;a, U &amp;b) { return std_pair_receiver&lt;T, U&gt;(a, b); } int main () { int x, y; receive_pair(x, y) = getvals(); //... } </code></pre> <p>If you have C++11 available to you, you can use the more general <code>tuple</code> and the <code>tie</code> helper to do this similarly in a more clean way. This is illustrated in Benjamin Lindley's answer.</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. 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.
 

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