Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look to the modified code of your's</p> <pre><code>#include &lt;iostream&gt; using namespace std; class ABC { char c; public: ABC() { cout &lt;&lt; "default" &lt;&lt; endl; } ABC(char c) { cout&lt;&lt;"parameterized constructor called\n";/////overloaded constructor called for the first line in main this-&gt;c = c; cout &lt;&lt; c &lt;&lt; endl; } ABC(ABC &amp;c) { cout&lt;&lt;"Copy cons\n";//copy constructor is called for the second line in main } ~ABC() { cout &lt;&lt; hex &lt;&lt; this &lt;&lt; " destructor " &lt;&lt; c &lt;&lt; endl; } void method() { cout &lt;&lt; "method" &lt;&lt; endl; } void operator= (const ABC&amp; a) { } }; int main() { ABC b('b');//////here parameterized constructor is called i.e &lt;ABC(char c)&gt; ABC a = b;/////////Here the copy constructor is called not the default one.(total 2 object created so the destructor is called twice!) } </code></pre> <p>The output of the program is</p> <pre><code>parameterized constructor called b Copy cons 0x7fff5fbff820 destructor � 0x7fff5fbff828 destructor b </code></pre> <p>Now lets explain why copy constructor is called in 3 cases 1.When an object in initialized 2.When an object is passed as parameter to a function 3.when an object is returned from a function.</p> <p>If you not specify your own copy constructor then compiler implement its own copy constructor which copy the object bit by bit. You have not specify your own copy constructor thats why you can not track the two object created from the code. Thanks</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. 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