This is not a copy constructor. A copy constructor takes a Matrix4 as a parameter (reference, const reference).
Second, that parameter, regardless of how many dimensions you stick inside of the [][], all degrades to a pointer. There is no guarantee that what is passed is actually has 4 rows of floats.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 10th, 2012 at 03:54 AM.
Whenever you get a compiler error, it is imperative that you post a valid example. Your example contained numerous errors, and who knows if one error triggers other errors that are unrelated. For example, your constructor is not public, so how can you call it from any function? I made it public, added the appropriate headers, a main() program, and everything compiles successfully.
So change the code above to something that duplicates the error and will compile on everyone's system without having to introduce directX types or mystery functions that no one knows about.
moreover, note that the parameter "mat" in "Matrix4(float mat[4][4])" does not have type float[4][4] as you might think. Its type is float(*)[4]; this means that you can pass an array float[1][4], float[3][4], etc... to that constructor without any complaint from the compiler but with disastrous effects at runtime.
If you want type checking either pass by reference-to-array or properly write a copy constructor, as suggested by Paul.
I'd like to pass it as reference-to-array
But I can't do this
Matrix(const float& mat[][4])
which is a syntax error
Instead of messing around with naked, dumb arrays, use std::array and you get no issues:
Code:
#include <array>
// use std::array if using a more modern compiler instead of std::tr1::array
typedef std::tr1::array<float, 4> Float1D;
typedef std::tr1::array<Float1D, 4> Float2D;
class Matrix4
{
public:
Matrix4(Float2D& mat) : m(mat)
{ }
Matrix4() {}
Float2D m;
};
int main()
{
Float1D m1;
Float2D m;
m1.assign(10); // set the array up here
m.assign(m1);
Matrix4 matProj(m);
}
This solves the issue of passing by reference, and also addresses the disastrous effects if somewhere in your code you have declared anything but 4 rows and you inadvertently pass it to your functions. The std::array class is a lightweight STL class that wraps an array declaration. So all that's done is to create an array of an array.
If your Matrix class only consists of a 4x4 float array, all you really need is std::array as outlined above, and just typedef it to Matrix4.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 10th, 2012 at 07:43 AM.
Bookmarks