Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Initialization</strong> is creating an instance(of type) with certain value.</p> <pre><code>int i = 0; </code></pre> <hr> <p><strong>Assignment</strong> is to give value to an already created instance(of type).</p> <pre><code>int i; i = 0 </code></pre> <h2>To Answer your edited Question:</h2> <p><em><strong>What is the difference between Initializing And Assignment inside constructor? &amp;<br> What is the advantage?</em></strong> </p> <p>There is a difference between Initializing a member using initializer list and assigning it an value inside the constructor body. </p> <p>When you initialize fields via initializer list the constructors will be called once. </p> <p>If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.</p> <p>As you see there is an additional overhead of creation &amp; assignment in the latter, which might be considerable for user defined classes. </p> <p>For an integer data type or POD class members there is no practical overhead.</p> <hr> <p><strong>An Code Example:</strong> </p> <pre><code>class Myclass { public: Myclass (unsigned int param) : param_ (param) { } unsigned int param () const { return param_; } private: unsigned int param_; }; </code></pre> <p>In the above example: </p> <pre><code>Myclass (unsigned int param) : param_ (param) </code></pre> <p>This construct is called a <strong>Member Initializer List</strong> in C++.</p> <p>It <strong>initializes</strong> a member <code>param_</code> to a value <code>param</code>.</p> <hr> <p><em><strong>When do you <code>HAVE TO</code> use member Initializer list?</em></strong><br> You will have(rather forced) to use a Member Initializer list if: </p> <p>Your class has a reference member<br> Your class has a const member or<br> Your class doesn't have a default constructor</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. 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