title: How to correctly initialize, assign and use a dynamic Eigen matrix as a class member in C++?
body: <p>I have a class that has a dynamic Eigen matrix. I want to read a Matrix from a text file and assign it to the class matrix variable. What is the best way to do this? This is what I do at the moment </p>
  
  <pre><code>class MyFoo
  {
      MyFoo();
      Eigen::MatrixXd mat;
      void setMatrix();
  }
  
  MyFoo::MyFoo(): mat(Eigen::MatrixXd::Zero())
  {};
  
  void MyFoo::setMatrix()
  {
      mat = HelperLib::readMatFromTXTfile("someFileName");
  }
  
  // Eigen::MatrixXd HelperLib::readMatFromTXTfile(const string &amp;fname);
  </code></pre>
  
  <p>My problems are this: first of all the initialization doesn't seem right. Secondly, using the = to assign the read matrix doesn't not seem too optimal either. Unfortunately there aren't many examples on the Eigen homepage for this or I am too much of a noob to understand them.</p>