0

I have an inherited class for making complex matrices (from a parent matrix class). The idea was to create two objects from the parent class for real and complex parts of the matrix. I am kind of confused on how to make the constructor. The code is :

template <class type>
class complexMatrix: public matrix<type> 
{
public:

  matrix<type> Real;
  matrix<type> Complex;

  complexMatrix() //Default Constructor
  {
    matrix<type> Real;// Call the matrix class constructor by default
    matrix<type> Complex;
  }
  complexMatrix(int rows,int columns, string name) //Creat a complex matrix
  {
    string name_real,name_complex;
    name_real = name;
    name_complex = "i"+name;
    matrix<type> Complex(rows,columns,name_complex); // Create Imaginary matrix
    matrix<type> Real(rows,columns,name_real);
  }
  void complexrandomize()
  {
    Real.matrix<type>::randomize();
    Complex.matrix<type>::randomize();
  }

};

This code obviously doesn't work. In an answer I found here on stackoverflow, I understood that I can initialize two objects from the parent and then call it using Real(rows,columns,name). In my case however, this won't work because I needed the () operator to be overloaded. So that solution is out of the question. Another solution I can think of is creating the objects Real and Complex within the constructor and manually copying all the values in the Real and Complex member objects. This doesn't sound like a great solution somehow.

Does anyone have a better way to go about this problem?

Community
  • 1
  • 1
Mathews_M_J
  • 457
  • 4
  • 15
  • Use the [member initializer-list](http://en.cppreference.com/w/cpp/language/initializer_list). – David G Nov 16 '14 at 00:07
  • Could you explain a bit more on how to ? I am unfamiliar with initialiser lists. – Mathews_M_J Nov 16 '14 at 00:14
  • Your code needs to use the member initializer-list because your class is trying to call the constructors of its data-members. So in the default-constructor you need `complexMatrix() : Real(), Complex() {}` which will call the default-constructors of your two data members. As for the second constructor, it would be `complexMatrix(…) : Real(rows, columns, name), Complex(rows, columns, "i"+name) {}` – David G Nov 16 '14 at 00:25

1 Answers1

1

Use an initializer list: see here for more information.

template <class type>
class complexMatrix: public matrix<type> 
{
public:

  matrix<type> Real;
  matrix<type> Complex;

  complexMatrix() : Real(),Complex() // Call the matrix class constructor by default
  {
  }
};
druckermanly
  • 2,694
  • 15
  • 27
  • It would be better if he `= default`'ed the constructor. – David G Nov 16 '14 at 00:15
  • I agree in this case, but we don't know if he is using a C++11 compiler. Also, if he wants to do something nontrivial with the default constructor, he can't `=default` the constructor. For the purposes of learning initializer lists alone, I think this answer suffices. – druckermanly Nov 16 '14 at 00:19
  • I tried it out and it worked. I think I am using C++11 compiler, although I am not so sure how to check with my g++. Thanks a lot for this. If not this what other ways are there? I also tried by creating a pointer to an object and then initializing it to the address of the objects constructed from the parent class. Valgrind started acting all weird and I abandoned that path. – Mathews_M_J Nov 16 '14 at 00:30
  • You can write `complexMatrix() = default;` and it will create the constructor for you, just remove the whole constructor I wrote above and replace it with that. However, if you want your constructor to do something non-trivial, you can't use `=default;` – druckermanly Nov 16 '14 at 00:42