Sign in to follow this  
johnstanp

C++ cast operator overloading

Recommended Posts

Hello everybody. I've rewritten a bunch of C functions replacing all double*[3], double[3] variables by Matrix3 and Vector3 ones( the code was downloaded, so it's not mine ). I think I could have simply overloaded some cast operator. It's quite simple for the Vector3 class, but I haven't found a way to do that with the Matrix3 class( I hope it's actually possible ). If it is, then I simply want to know the syntax to do such a thing, i.e casting a Matrix3 type to a double*[3] "type". Here is the simplified Matrix3 class:
typedef double Real;

class Matrix3
{
	public:
		Matrix3();
                Matrix3( Real, Real, Real, Real, Real, Real, Real, Real, Real );
		Matrix3( Real[3][3] );

	private:
		Real m_[3][3];
};




Thanks beforehand for replying.

Share this post


Link to post
Share on other sites
Assuming I understand what you're asking:

typedef float Real;
typedef Real Real3x3[3][3];

class Matrix3
{
public:
Matrix3();
Matrix3( Real[3][3] );

operator Real3x3 *() { return &m_; }

private:
Real m_[3][3];
};

Share this post


Link to post
Share on other sites
Your assumption is correct...
As you certainly know the compiler refused:


typedef double Real;
typedef Real Real3x3[3][3];

class Matrix3
{
public:
Matrix3(){}

operator Real*[3](){
return m_;
}

operator Real3x3(){
return m_;
}

private:
Real m_[3][3];
};









/home/stanp/Bureau/ngn1/src/main.cpp|52|erreur: function definition does not declare parameters|
/home/stanp/Bureau/ngn1/src/main.cpp|56|erreur: ‘<invalid operator>’ declared as function returning an array|
||=== Build finished: 3 errors, 0 warnings ===|


Hence the need to return a pointer to an array and to typedef Real typename[3][3] to avoid the first compile error. Do I get it right?


P.S: thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this