Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your options are to return by value or to return a reference/pointer to a heap-based object.</p> <h2>Return by value</h2> <p>Changing your function signature to this</p> <pre><code>Test returnval() </code></pre> <p>will copy <code>obj</code>. Note that the pointers you print out may still have the same value for the object inside the class and the object outside, as the compiler may have performed a <a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/" rel="nofollow noreferrer">return value optimisation</a>.</p> <p>If the <code>Test</code> class is not managing dynamically allocated resources, then you can rely on the automatically created copy constructors that the compiler will inject. If <code>Test</code> has dynamically allocated data, then you must write your own. See <a href="https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three">What is the Rule of Three?</a>.</p> <h2>Return a pointer (or reference) to a heap-based object</h2> <p>You can change it to a heap-based object by using <code>new</code>, and then return a pointer instead: </p> <pre><code>Test* returnptr(){ Test* obj = new Test(9,9); cout &lt;&lt; "in function: " &lt;&lt; obj &lt;&lt; endl; return obj; } </code></pre> <p>Or better yet, a smart pointer like shared_ptr to manage the deletion for you:</p> <pre><code>shared_ptr&lt;Test&gt; returnptr() { // Wrapping the pointer in a shared_ptr will ensure it gets cleaned // up automatically when the last reference to it (usage of it) // goes out of scope. shared_ptr&lt;Test&gt; obj(new Test(9,9)); cout &lt;&lt; "in function: " &lt;&lt; obj.get() &lt;&lt; endl; return obj; } </code></pre> <h2>Final note</h2> <p>As pointed out by one of the commenters on my answer, in C++11, you have further options to control how the temporary object from your function is returned by providing move constructors for <code>Test</code> and using <code>std::move</code> as necessary. This is a fairly meaty subject, but you can read more about it at the following links:</p> <ul> <li><a href="https://stackoverflow.com/questions/3106110/what-is-move-semantics">What is move semantics?</a></li> <li><a href="http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" rel="nofollow noreferrer">Rvalue References: C++0x Features in VC10</a></li> <li><a href="http://www.artima.com/cppsource/rvalue.html" rel="nofollow noreferrer">A Brief Introduction to Rvalue References</a></li> </ul>
    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.
 

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