Jump to content
  • Advertisement
Sign in to follow this  
RetroBilly

How to make my class castable to another class?

This topic is 2745 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

I have two matrix classes that I want to be able to cast to each other but I don't know how to do that...

Here are the two classes:


class AxMatrix33
{
public:
union
{
struct
{
float _11, _12, _13;
float _21, _22, _23;
float _31, _32, _33;
};
float m[3][3];
};

AxMatrix33 operator * ( const AxMatrix33& ) const;
};

class AxMatrix44
{
public:
union
{
struct
{
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;
};
float m[4][4];
};

AxMatrix44 operator * ( const AxMatrix44& ) const;
};




One is a 3x3 matrix and the other is a 4x4 matrix. I simply want to drop the last row to convert the 4x4 to a 3x3 and add a row to cast the 3x3 to a 4x4. The classes that I have shown are inside a header and the actual implementation of the code will be inside the CPP file.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by TerranceN
Try reading this.


I read it all the way through but I don't see how that is going to help me. Maybe I'm not understanding it but I don't see anywhere in that article that explains how to actually define how to cast the type... I'm thought it would work something like an operator.

Share this post


Link to post
Share on other sites
Add some constructors and assignment operators:
class AxMatrix44;
class AxMatrix33
{
public:
AxMatrix33( const AxMatrix44& );
AxMatrix33& operator=( const AxMatrix44& );
};
class AxMatrix33
{
public:
AxMatrix44( const AxMatrix33& );
AxMatrix44& operator=( const AxMatrix33& );
};

Share this post


Link to post
Share on other sites
isn't the assignment operator not unnecessary?

Since the constructor isn't explicit, the casting can be done implicitelly and hence casting can be done automatically by the compiler when assigning.

Share this post


Link to post
Share on other sites
You can't cast from a 4x4 matrix to a 3x3 because a 4x4 matrix isn't a superset in terms of memory layout. The m[4][4] is a contiguous set of floats, so regardless of where you slice the memory, you won't get 9 floating point values referring to a rectangular region of the 4x4 matrix.

It is possible however, to extract the 4 component row/column vectors from the matrix via typecasting. Either the row vectors or the column vectors will be contiguous depending on how you interpret the data in the matrix (row major vs column major)

Edit: You don't need to typecast to extract vectors, provided you define the matrix as an array of 3/4 component vectors to begin with.

Share this post


Link to post
Share on other sites
Please explain, in detail, exactly what you expect to happen when you "cast" a 3x3 matrix to a 4x4 matrix, and vice-versa. Please also explain why such behaviour is "obvious" enough to justify making it happen automatically by casting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Please explain, in detail, exactly what you expect to happen when you "cast" a 3x3 matrix to a 4x4 matrix, and vice-versa.
Quote:
Original post by SteveDeFacto
I simply want to drop the last row to convert the 4x4 to a 3x3 and add a row to cast the 3x3 to a 4x4.

Quote:
Please also explain why such behaviour is "obvious" enough to justify making it happen automatically by casting.
See HLSL's usage.

Share this post


Link to post
Share on other sites
Just because nobody has brought this up and it bugs the hell out of me.


SteveDeFacto do you realise that both the definition of your union and almost certainly the usage of it is either implementation specific or undefined in current C++?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!