It's likely that what I'm trying to do is terrible practice. If so please tell me so I can stop. Anyway, I'm working on some basic matrix math functions using dynamic memory to store the matrix data array. I'm trying to figure out how to convert a vector passed into a constructor to this array, but I get some odd results. Here's the simple version.
Header:
class Matrix {
public:
unsigned int size = 4;
float *M;
Matrix(std::vector<float> values) : M(&values[0]){}
~Matrix() = default;
};
And the test file:
int main(){
Matrix m(std::vector<float>{ 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 2.0f, 3.0f, 4.0f,
5.0f, 6.0f, 7.0f, 8.0f,
9.0f, 10.0f, 11.0f, 12.0f });
return 0;
}
The result is...wrong. Everything seemingly works (from watching the values while debugging) until the return from the constructor. My guess is Visual Studio's debugger is lying to me though and it happens earlier than that.
So aside from the fact that the size isn't being changed, what's going on? Is there a better way to do this? Also, is it possible to accept a std::initializer_list and give that data to the dynamic pointer?