So, to the best of my knowlege I've done this correctly, however I encountered some strange behavior last night and I'm trying to find a solution.
I have a 3x3 Matrix class,
Mat3f, comprised of floats. It has all the typical Matrix/Matrix and Matrix/Vector operations, as well as some optimized routines for transforming an array of vertices or vectors all at once.
It has a const static Mat3f member named
IDENTITY which is initialized in the .cpp to the 3x3 identity matrix using one of Mat3f's constructors.
While implimenting a Matrix Stack for 2D transforms last night, I noticed that the contents of my transformation matrix always contained the zero matrix. Eventually I traced this back to the fact that
IDENTITY did not contain the identity matrix as I assumed it would, but contained the zero matrix instead. Because the transformation matrix was initially set to
IDENTITY, subsequent multiplies by various transform matrices always resulted (correctly) in a zero matrix.
Are there any rules or gotchas of which I am obviously unaware in regards to const static members? I wonder if its perhaps due to the fact that the member in question is of the same type as the class containing it? Maybe because the class must be fully defined (The class is pre-declared in the header as well) before one can initialize it? If so, is there any way around it?
Here is a snippet of the header:
// ... more ...
class Mat3f;
// ... some friend function declarations ...
class Mat3f
{
public:
Mat3f() { };
Mat3f(const float, const float, const float,
const float, const float, const float,
const float, const float, const float);
// ... method & operator declarations ...
// Usefull constants
static const Mat3f IDENTITY;
static const Mat3f ZERO;
float m11, m21, m31,
m12, m22, m32,
m13, m23, m33;
};
// ... more ...
And here is the initialization statement from the .cpp file:
#include "mat3.hpp"
const Mat3f Mat3f::IDENTITY = Mat3f(1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f);
Mat3f::Mat3f(const Mat3f &src)
{
m11 = src.m11; m12 = src.m12; m13 = src.m13;
m21 = src.m21; m22 = src.m22; m23 = src.m23;
m31 = src.m31; m32 = src.m32; m33 = src.m33;
}
Mat3f::Mat3f(const float p11, const float p12, const float p13,
const float p21, const float p22, const float p23,
const float p31, const float p32, const float p33)
{
m11 = p11; m12 = p12; m13 = p13;
m21 = p21; m22 = p22; m23 = p23;
m31 = p31; m32 = p32; m33 = p33;
}
I'm using Microsoft Visual Studio 2005 Professional (native C++)
[edit]Whoops! Added constructors at Apoch's request.
[Edited by - ravyne2001 on March 26, 2007 9:45:44 PM]