title: Why is conversion constructor called in absense of assignment operator?
body: <p>Today my question is why is a constructor called in absence of a user provided assignment operator? I know the difference between constructor and assignment operator in relation to the <code>=</code> symbol. If an object did not exist previously before that line, the constructor is implicitly called.</p>
  
  <p><code>std::string name = "Bjarne";  // implicit constructor</code></p>
  
  <p>If an object already existed previously before that line, then assignment operator is called.</p>
  
  <pre><code>std::string name;
  name = "Bjarne";   // assignment operator
  </code></pre>
  
  <p>Now I have a <code>class Number</code> here, which is a tagged union. It is simply a number, means either an <code>int</code> or a <code>float</code>. I have multiple conversion constructors defined which make it easy for the user to assign values to the <code>Number</code> implicitly. I expect them to be called in such situation:</p>
  
  <p><code>Number num1 = 8;</code></p>
  
  <p>In order to know for sure which constructor is called when, I put print statements in the bodies of the constructors.</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  using std::cin;
  using std::cout;
  using std::cerr;
  using std::endl;
  using std::ostream;
  
  
  /* Unions can be anonymous (unnamed).
   * This form is used when nesting a union inside a struct or class that contains an extra data member
   * to indicate what the union contains.
   * This arrangement is called a tagged union (or managed union).
   * It can be used as a "polymorphic" class which can be several things at once, for example to create an array
   * of different data types.
   */
  
  class Number {
      friend ostream&amp; operator&lt;&lt;(ostream&amp; os, Number&amp; num);
    public:
      Number(int i = 0) : DATA_TYPE(INTEGER), i(i) { cerr &lt;&lt; "Number(" &lt;&lt; i &lt;&lt; ")\n"; }
  
      Number(unsigned int u) : DATA_TYPE(INTEGER), i(static_cast&lt;int&gt;(u)) { cerr &lt;&lt; "Number((signed) " &lt;&lt; i &lt;&lt; ")\n"; }
  
      Number(float f) : DATA_TYPE(FLOAT), f(f) { cerr &lt;&lt; "Number(" &lt;&lt; f &lt;&lt; ")\n"; }
  
      Number(double d) : DATA_TYPE(FLOAT), f(static_cast&lt;float&gt;(d)) { cerr &lt;&lt; "Number((float) " &lt;&lt; f &lt;&lt; ")\n"; }    
  
      Number(const Number&amp; rhs) : DATA_TYPE(rhs.DATA_TYPE), i(rhs.i) { cerr &lt;&lt; "Number((Number) " &lt;&lt; *this &lt;&lt; ")\n"; }
    private:
          enum data_type { INTEGER, FLOAT } DATA_TYPE;
  
      union {
          int i;
          float f;
      };
  };
  
  
  ostream&amp; operator&lt;&lt;(ostream&amp; os, Number&amp; num)
  {
      switch (num.DATA_TYPE) {
          case Number::INTEGER:
        cout &lt;&lt; num.i;
            break;
          case Number::FLOAT:
        cout &lt;&lt; num.f;
            break;
      default:
        cout &lt;&lt; 0;
        break;
      }
  
      return os;
  }
  
  
  int main()
  {
      Number num1 = 5;  // conversion constructor
      cout &lt;&lt; num1 &lt;&lt; endl;
      num1 = 12.5;
      cout &lt;&lt; num1 &lt;&lt; endl;
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>When I run this code it produces unexpected output for me:</p>
  
  <pre><code>Number(5)
  5
  Number((float) 12.5)
  12.5
  </code></pre>
  
  <p>I see that when <code>Number num1</code> is initialized, the constructor taking an <code>int</code> as a parameter is called. Then I print the value of <code>num1</code>. Then on the next line it seems that the constructor taking a <code>double</code> as a parameter is called. This is strange. Before that line, <code>num1</code> already existed as an object. So it should logically call assignment operator instead. No assignment operator was defined by me instead, so I assume that the assignment operator generated by the compiler will be called.</p>
  
  <p>I want to know, in the absence of a user provided assignment operator, what happens when I do this? <code>num1 = something;</code> Does the default assignment operator provided by the compiler get called or does a constructor get called? What happens when the constructor is called on an object that already exists? Does that object get "re-constructed"? So what actually happens in that case? </p>