Note that there are some explanatory texts on larger screens.

plurals
  1. POoperator= become to copy construtor in c++11 test
    primarykey
    data
    text
    <p>I have the following test in g++ 4.8.1 :</p> <p>g++ -std=c++11 testclass.cpp -o testclass.exe</p> <pre><code>template&lt;typename T&gt; class XRef { private : int inum ; T * ptr ; bool owner ; public : XRef(int i,T *ptrx):inum{i},ptr{ptrx},owner{true} {cout &lt;&lt; "natural" &lt;&lt; endl ;} XRef(XRef&amp; x):inum{x.inum},ptr{x.ptr},owner{false} {cout &lt;&lt; "copy" &lt;&lt; endl ;} XRef&amp; operator=(XRef&amp; x) { inum = x.inum ; ptr = x.ptr ; owner = false ; cout &lt;&lt; "assign" &lt;&lt; endl ; return *this ; } XRef(XRef&amp;&amp; x):inum{x.inum},ptr{move(x.ptr)},owner{true} {cout &lt;&lt; "move" &lt;&lt; endl ;} ~XRef() { if(owner) delete ptr ; } } ; int main() { char *ptr1 ; char *ptr2 ; ptr1 = (char *) malloc(100) ; ptr2 = (char *) malloc(100) ; XRef&lt;char&gt; x1 = XRef&lt;char&gt;(1,ptr1) ; cout &lt;&lt;"==============" &lt;&lt; endl ; XRef&lt;char&gt; x2 = x1 ; cout &lt;&lt;"==============" &lt;&lt; endl ; XRef&lt;char&gt; x3(x2) ; cout &lt;&lt;"==============" &lt;&lt; endl ; XRef&lt;char&gt; x4(XRef&lt;char&gt;(123,ptr2)) ; cout &lt;&lt;"==============" &lt;&lt; endl ; XRef&lt;char&gt; x5(move(XRef&lt;char&gt;(123,ptr2))) ; cout &lt;&lt;"==============" &lt;&lt; endl ; XRef&lt;char&gt; x6{123,ptr2} ; } </code></pre> <p>Then , the output :</p> <pre><code>natural ============== copy ============== copy ============== natural ============== natural move ============== natural </code></pre> <p>What surprise me is that : XRef x2 = x1 ; , I think this should call XRef&amp; operator=(XRef&amp; x) , but this test showes it call XRef(XRef&amp; x) instead .... </p> <p>I like to know what i do is wrong , so that operator= is not called !!</p> <p>Edit :</p> <pre><code>XRef&lt;char&gt; x7{123,ptr2} ; cout &lt;&lt;"==============" &lt;&lt; endl ; x7 = x6 ; cout &lt;&lt;"==============" &lt;&lt; endl ; </code></pre> <p>Showes :</p> <pre><code>natural ============== assign ============== </code></pre> <p>So , </p> <pre><code>XRef&lt;char&gt; x2 = x1 ; </code></pre> <p>is different with</p> <pre><code>XRef&lt;char&gt; x7{123,ptr2} ; x7 = x6 ; </code></pre> <p>How come this happened ?</p> <p>PS. I refered to : <a href="https://stackoverflow.com/questions/16865239/copy-construtor-called-extra?rq=1">copy construtor called extra</a> for reference ...</p>
    singulars
    1. This table or related slice is empty.
    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