title: Differences of using "const cv::Mat &", "cv::Mat &", "cv::Mat" or "const cv::Mat" as function parameters?
body: <p>I've searched thoroughly and have not found a straightforward answer to this.</p>
  
  <p>Passing opencv matrices (<code>cv::Mat</code>) as arguments to a function, we're passing a smart pointer. Any change we do to the input matrix inside the function alters the matrix outside the function scope as well.</p>
  
  <p>I read that by passing a matrix as a const reference, it is not altered within the function. But a simple example shows it does:</p>
  
  <pre><code>void sillyFunc(const cv::Mat&amp; Input, cv::Mat&amp; Output){
      Output = Input;
      Output += 1;
  }
  
  int main( int argc, char** argv ){
      cv::Mat A = cv::Mat::ones(3,3,CV_8U);
      std::cout&lt;&lt;"A = \n"&lt;&lt;A&lt;&lt;"\n\n";
      cv::Mat B;
      sillyFunc(A,B);
      std::cout&lt;&lt;"A = \n"&lt;&lt;A&lt;&lt;"\n\n";
      std::cout&lt;&lt;"B = \n"&lt;&lt;B&lt;&lt;"\n\n";
  }
  </code></pre>
  
  <p>Clearly, <code>A</code> is altered even though it is sent as a <code>const cv::Mat&amp;</code>.</p>
  
  <p>This does not surprise me as within the function <code>I2</code> is a simple copy of <code>I1</code>'s smart pointer and thus any change in <code>I2</code> will alter <code>I1</code>.</p>
  
  <p>What does baffle me is that I don't understand what practical difference exists between sending <code>cv::Mat</code>, <code>const cv::Mat</code>, <code>const cv::Mat&amp;</code> or <code>cv::Mat&amp;</code> as arguments to a function.</p>
  
  <p>I know how to override this (replacing <code>Output = Input;</code> with <code>Output = Input.clone();</code> would solve the problem) but still don't understand the above mentioned difference.</p>
  
  <p>Thanks guys!</p>