title: Overloading operator << - C++
body: <p><strong>Background</strong></p>
  
  <p>I have a container class which uses vector&lt;std::string&gt; internally. I have provided a method <em>AddChar(std::string)</em> to this wrapper class which does a <em>push_back()</em> to the internal vector. In my code, I have to add multiple items to the container some time. For that I have to use </p>
  
  <pre><code>container.AddChar("First");
  container.AddChar("Second");
  </code></pre>
  
  <p>This makes the code larger. So to make it more easier, I plan to overload operator &lt;&lt;. So that I can write</p>
  
  <pre><code>container &lt;&lt; "First" &lt;&lt; "Second"
  </code></pre>
  
  <p>and two items will get added to underlying vector.</p>
  
  <p>Here is the code I used for that</p>
  
  <pre><code>class ExtendedVector
  {
  private:
      vector&lt;string&gt; container;
  
  public:
      friend ExtendedVector&amp; operator&lt;&lt;(ExtendedVector&amp; cont,const std::string str){
          cont.AddChar(str);
          return cont;
      }
  
      void AddChar(const std::string str)
      {
          container.push_back(str);
      }
  
      string ToString()
      {
          string output;
          vector&lt;string&gt;::iterator it = container.begin();
          while(it != container.end())
          {
              output += *it;
              ++it;
          }
          return output;
      }
  };
  </code></pre>
  
  <p>It is working as expected. </p>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>Is operator overload written correctly?</li>
  <li>Is it a good practice to overload operators in situations like this?</li>
  <li>Will there be any performance issues or any other issues with this code?</li>
  </ol>
  
  <p>Any thoughts?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After hearing the excellent comments, I decided not to overload &lt;&lt; as it doesn't make sense here. I removed the operator overload code and here is the final code.</p>
  
  <pre><code>class ExtendedVector
  {
  private:
      vector&lt;string&gt; container;
  
  public:
  
      ExtendedVector&amp; AddChar(const std::string str)
      {
          container.push_back(str);
          return *this;
      }
  
           .. other methods
  }
  </code></pre>
  
  <p>This allows me to add </p>
  
  <pre><code>container.AddChar("First").AddChar("Second")
  </code></pre>
  
  <p>In C#, I can do this more easily by using the params keyword. Code will be like</p>
  
  <pre><code>void AddChar(params string[] str)
  {
      foreach(string s in str)
         // add to the underlying collection
  }
  </code></pre>
  
  <p>I know in C++, we can use <em>...</em> to specify variable langth of parameters. But AFAIK, it is not type safe. So is it a recommended practice to do so? So that I can write</p>
  
  <pre><code>container.AddChar("First","Second")
  </code></pre>
  
  <p>Thanks for the replies.</p>