title: OpenCV C++ Mat class rows and cols - are they member variables (and related questions)?
body: <p>I'm experiencing some confusion on how rows and cols are implemented in OpenCV's Mat class, hopefully somebody can provide some clarification.</p>
  
  <p>When using the Mat class, rows and cols can't have () after them, i.e.:</p>
  
  <pre><code>cv::Mat imgSomeImage;
  imgSomeImage = cv::imread("some_image.png");
  
  // this line works
  std::cout &lt;&lt; "num rows = " &lt;&lt; imgSomeImage.rows &lt;&lt; "\n";
  
  // this line does not compile, only difference is the () after rows
  std::cout &lt;&lt; "num rows = " &lt;&lt; imgSomeImage.rows() &lt;&lt; "\n";
  </code></pre>
  
  <p>Having familiarity with .NET, I at first figured rows and cols must be properties, but after reading this:</p>
  
  <p><a href="https://stackoverflow.com/questions/8368512/does-c11-have-c-style-properties">Does C++11 have C#-style properties?</a></p>
  
  <p>it seems C++ does not have an equivalent, at least not without adding a class to mimic .NET properties, which as far as I could find, OpenCV does not do.</p>
  
  <p>So, I figured Mat rows and cols must be member variables, and went to the OpenCV source for confirmation.  </p>
  
  <p>Checking mat.hpp:</p>
  
  <p><a href="https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/mat.hpp" rel="nofollow noreferrer">https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/mat.hpp</a></p>
  
  <p>line 217 and 218:</p>
  
  <pre><code>int cols(int i=-1) const;
  int rows(int i=-1) const;
  </code></pre>
  
  <p>is where I get unclear on things.  I've seen this many times:</p>
  
  <pre><code>// declare a member variable with a default value of -1
  int cols = -1;
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>const int SOME_CONSTANT = 123;
  </code></pre>
  
  <p>and if cols should be read-only to the outside world, I would have figured something like this:</p>
  
  <pre><code>// member variable
  private:
      int _cols;
  
  // getter
  public:
      int cols() { return _cols; }
  </code></pre>
  
  <p>Looking at the usage of rows and cols in matrix.cpp:</p>
  
  <p><a href="https://github.com/opencv/opencv/blob/master/modules/core/src/matrix.cpp" rel="nofollow noreferrer">https://github.com/opencv/opencv/blob/master/modules/core/src/matrix.cpp</a></p>
  
  <p>for example, line 395:</p>
  
  <pre><code>    if( d == 2 &amp;&amp; rows == _sizes[0] &amp;&amp; cols == _sizes[1] )
  </code></pre>
  
  <p>or line 498:</p>
  
  <pre><code>    cols = _colRange.size();
  </code></pre>
  
  <p>and many similar examples, it seems these are indeed member variables, but I still am unclear on the line 217 &amp; 218 syntax:</p>
  
  <pre><code>int cols(int i=-1) const;
  int rows(int i=-1) const;
  </code></pre>
  
  <p>Can somebody clarify if these are member variables and what is going on syntax-wise on the declaration line?</p>