Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat happens to the return value( which returns an object ) when it is neglected?
    primarykey
    data
    text
    <p>I was going through this particular example in Thinking in C++- Bruce Eckel.I could understand the <strong>statement 1(mentioned in comments ) and statement 2</strong> but had a hard time understanding the <strong>statement 3</strong> when there is no return even if the function declaration and definition requires an object to be returned for copy purposes. Now here what actually happens? For the other two statements (1 and 2) what I could infer was that <em>compiler prevents bitcopying because we have specified a copy constructor inside our class and instead handles it through the copy-constructor defined by us for the object passed inside the function as well as for the object returned by the function. Just before the end of function the temporary object inside the function is copied for the return value and then destructed</em>. Am I right in this?</p> <pre><code>#include &lt;fstream&gt; #include &lt;string&gt; using namespace std; ofstream out("HowMany2.out"); class HowMany2 { string name; // Object identifier static int objectCount; public: HowMany2(const string&amp; id = "") : name(id) { ++objectCount; print("HowMany2()"); } ~HowMany2() { --objectCount; print("~HowMany2()"); } // The copy-constructor: HowMany2(const HowMany2&amp; h) : name(h.name) { name += " copy"; ++objectCount; print("HowMany2(const HowMany2&amp;)"); } void print(const string&amp; msg = "") const { if(msg.size() != 0) out &lt;&lt; msg &lt;&lt; endl; out &lt;&lt; '\t' &lt;&lt; name &lt;&lt; ": "&lt;&lt; "objectCount = "&lt;&lt; objectCount &lt;&lt; endl; } }; int HowMany2::objectCount = 0; // Pass and return BY VALUE: HowMany2 f(HowMany2 x) { x.print("x argument inside f()"); out &lt;&lt; "Returning from f()" &lt;&lt; endl; return x; } int main() { HowMany2 h("h");//statement 1 out &lt;&lt; "Entering f()" &lt;&lt; endl; HowMany2 h2 = f(h);//statement 2 h2.print("h2 after call to f()"); out &lt;&lt; "Call f(), no return value" &lt;&lt; endl; f(h);//statement 3 out &lt;&lt; "After call to f()" &lt;&lt; endl; } </code></pre> <p>According to Eckel , the output is:</p> <pre><code>HowMany2() h: objectCount = 1 Entering f() HowMany2(const HowMany2&amp;) h copy: objectCount = 2 x argument inside f() h copy: objectCount = 2 Returning from f() HowMany2(const HowMany2&amp;) h copy copy: objectCount = 3 ~HowMany2() h copy: objectCount = 2 h2 after call to f() h copy copy: objectCount = 2 Thinking in C++ www.BruceEckel.com Call f(), no return value HowMany2(const HowMany2&amp;) h copy: objectCount = 3 x argument inside f() h copy: objectCount = 3 Returning from f() HowMany2(const HowMany2&amp;) h copy copy: objectCount = 4 ~HowMany2() h copy: objectCount = 3 ~HowMany2() h copy copy: objectCount = 2 After call to f() ~HowMany2() h copy copy: objectCount = 1 ~HowMany2() h: objectCount = 0 </code></pre> <p>Also why couldn't we allocate extra storage for the return values so that we can store them there before a function call. Could it be an alternative to using references? Thank you in advance!!!</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.
 

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