Jump to content
  • Advertisement
Sign in to follow this  
johnstanp

C++ cast operator overloading

This topic is 3334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!