title: C++ - Unable to overload assignment operator
body: <p>I am trying to overload the assignment operator but it doesn't seem to work. <a href="https://stackoverflow.com/a/7880348/3895469">The code is based on this answer</a>. I've searched for other examples of overloading the assignment operator, but it doesn't seem like my code shouldn't run.</p>
  
  <p>This is my code:</p>
  
  <pre><code>#pragma once
  
  #include &lt;assert.h&gt;
  
  class ReadOnlyInt
  {
  public: 
      ReadOnlyInt(): assigned(false) {}
  
      ReadOnlyInt&amp; operator=(int v);
      operator int() const ;
  private: 
      int value; 
      bool assigned; 
  }; 
  
  ReadOnlyInt&amp; ReadOnlyInt::operator=(int v)
  {
      assert(!assigned); 
      value = v; 
      assigned = true; 
      return *this; 
  }   
  
  ReadOnlyInt::operator int() const 
  { 
      assert(assigned); 
      return value; 
  }
  </code></pre>
  
  <p>Intellisense doesn't give any warnings, but the <code>operator=</code> is not highlighted as a keyword.</p>
  
  <p>Now if I make an assigment, Intellisense does recognize it's not possible:</p>
  
  <pre><code>ReadOnlyInt bar = 12;
  </code></pre>
  
  <blockquote>
    <p>no suitable constructor exists to convert from "int" to "ReadOnlyInt"</p>
  </blockquote>
  
  <p>This works however:</p>
  
  <pre><code>int foo = bar;
  </code></pre>
  
  <h1>Solution</h1>
  
  <p>This question was marked as duplicate, so I can't answer it. This was the solution I came up with based on the comments and answer on this question:</p>
  
  <pre><code>ReadOnlyInt::ReadOnlyInt()
  : assigned(false)
  {}
  
  ReadOnlyInt::ReadOnlyInt(int v)
  : value(v), assigned(true)
  {}
  </code></pre>