Re: Strange protection error
SquareMatrix<int, 3> and SquareMatrix<int, 2> are (almost) unrelated types. They do share the same base class but there is no other connection between them: they are not friends, they do not inherit each etc. So there is no reason for SquareMatrix<int, 3> to access the protected/private members from SquareMatrix<int, 2>.
Re: Strange protection error
Thanks, it makes sense now. Do you think I should provide an accessor function to a pointer of the first element of the array or somehow make them friends?
Re: Strange protection error
SquareMatrix<int,3,3> has access to Matrix<int,3,3> protected members (since it is derived from it), but not to protected members of Matrix<int,2,2>
Your program has flaws.
SquareMatrix should not derive from Matrix!
In fact Matrix is a useless class, since it has no public interface, and thus, can't be a useful base class.
Moreover your code uses public inheritance, while private inheritance would be better : If someone derive from your matrix, it should not be able to access implementation details.
You should rather put m as a public member of Matrix, and Matrix as a private member of SquareMatrix.
Quote:
int *pretm = (int*)&ret.m[0][0]; //compiler hates this
Incorrect! &ret.m[0][0] is a int (*)[dim2]. It can't be safely converted to int*, because there may be some padding at the end of an array.
This should be *pretm = this->m[u][v];
Other compile-time errors are trivial...
Re: Strange protection error
Wow, I'm glad I posted. There are actually a lot more functions in the Matrix class than I posted. I wanted to make looking through the code easier. I was actually going to implement operator overloads for addition and multiplication in the base class, since this is a generic property between matrices. That's why I was using public inheritance. Otherwise, I would have to implement an arithmetic operators in every derived class.