title: C++ Always Const Reference in Constructor?
body: <p>So consider following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class A {
  public:
      A() = default;
  
      A(const A&amp; rhs) {
          cout &lt;&lt; "Copy was made!" &lt;&lt; endl;
      }
  };
  
  class B {
  public:
      A data;
      int foo;
  
      B(A data, int foo) : data(data), foo(foo) {
      }
  };
  
  int main() {
      A data;
  
      B foo(data, 10);
  
      return 0;
  }
  </code></pre>
  
  <p>This prints out:</p>
  
  <blockquote>
    <p>Copy was made!</p>
    
    <p>Copy was made!</p>
  </blockquote>
  
  <p>Yes thats right, it copies twice!</p>
  
  <p>The first copy happends when we pass <code>data</code> to <code>B's</code> constructor.
  The second copy happends when we copy <code>data</code> from the constructor to the memeber variable.</p>
  
  <p>We know that we can not go under 1 copy (unless we go <code>heap &amp; pointers</code>). So why don't we always write:</p>
  
  <p><code>B (const A&amp; data, const int&amp; foo, const SomeOtherType&amp; bar, const float&amp; aFloatyNumber)</code> ... and so on.</p>
  
  <p>I know that it's cheap to pass int, float etc by value. But by always having <code>const ref</code> as <code>Constructor</code> parameters we will guarantee 1 fewer copies.</p>