Casting to a 'different' class...

N

nl

Hello,

I've got a question... I'm currently coding a software rasterizer...
I actually like to write my own matrix/vector classes, which I did, but
those classes don't work on the D3DX library of DirectX functions which I
also like to use... These only accept D3DXMATRIX, D3DXVECTOR2/3/4 etc
classes...

Using casting, is there any way to cast forward and backward from my own
classes to and from the microsoft d3dx classes? I'm thinking yes... almost
sure yes, but I'm not sure how to do it... so a code example would be
welcome...

Thx in advance...
 
K

Karl Heinz Buchegger

nl said:
Hello,

I've got a question... I'm currently coding a software rasterizer...
I actually like to write my own matrix/vector classes, which I did, but
those classes don't work on the D3DX library of DirectX functions which I
also like to use... These only accept D3DXMATRIX, D3DXVECTOR2/3/4 etc
classes...

Using casting, is there any way to cast forward and backward from my own
classes to and from the microsoft d3dx classes? I'm thinking yes... almost
sure yes, but I'm not sure how to do it... so a code example would be
welcome...

A cast is the moral equivalent of telling the compiler:
Shut up!

In your case the cast will shut up the compiler error messages.
But that won't help you very much. If the data structures are
incompatible, they are incompatible. If you shut up the compiler
or not.
 
U

Unforgiven

nl said:
Hello,

I've got a question... I'm currently coding a software rasterizer...
I actually like to write my own matrix/vector classes, which I did,
but those classes don't work on the D3DX library of DirectX functions
which I also like to use... These only accept D3DXMATRIX,
D3DXVECTOR2/3/4 etc classes...

Using casting, is there any way to cast forward and backward from my
own classes to and from the microsoft d3dx classes? I'm thinking
yes... almost sure yes, but I'm not sure how to do it... so a code
example would be welcome...

Something like this:
class MyMatrix
{
MyVector(const D3DXMATRIX &mat)
{ /* copy contents of D3DXMATRIX into this class for initialization */ }

MyMatrix& operator=(const D3DXMATRIX &mat)
{ /* copy contents of D3DXMATRIX into this class for assignment */
return *this;
}

operator D3DXMATRIX()
{ /* fill in a new D3DXMATRIX with data from this class, and return it
*/ }
}

This will allow you to cast your class to and from a D3DXMATRIX.
 
U

Unforgiven

Unforgiven said:
Something like this:
class MyMatrix
{
MyVector(const D3DXMATRIX &mat)
{ /* copy contents of D3DXMATRIX into this class for
initialization */ }

Uhm... the constrcutor must be called MyVector of course, not MyMatrix...
*sheepish grin*
 
U

Unforgiven

Unforgiven said:
Uhm... the constrcutor must be called MyVector of course, not
MyMatrix... *sheepish grin*

God I did it again, it must be called *MyMatrix*. It's way to late at night
for me to be doing this obviously...
And it's spelled constructor of course, I'm on a roll today... a bad one,
unfortunately.
 
D

David Fisher

Unforgiven said:
God I did it again, it must be called *MyMatrix*. It's way to late at night
for me to be doing this obviously...
And it's spelled constructor of course, I'm on a roll today... a bad one,
unfortunately.

You are forgiven ! :)

David F
 
U

Unforgiven

David said:
You are forgiven ! :)

It may be hard to believe, but in 4 years using this name on Usenet you're
only the second person ever to make that joke. ^_^
 
R

Rolf Magnus

Unforgiven said:
It may be hard to believe, but in 4 years using this name on Usenet
you're only the second person ever to make that joke. ^_^

You are forgiven!

Am I added as number 3 now to the list? Do I win something? :)
 
J

Jerry Coffin

[email protected] says... said:
Using casting, is there any way to cast forward and backward from my own
classes to and from the microsoft d3dx classes?

I'm not sure about "forward and backward", but it's certainly possible
to support conversion between the two types. Normally, you'll want to
support conversion from their type to yours by writing a ctor that takes
an object of their type as a parameter.

Conversion from their type to yours is done by writing a cast operator
in your class that does whatever is needed to covert your data to their
format. A really simple case could be done like this:

class Integer {
int val;
public:
Integer(int init) : val(init) {}

operator int() { return val; }
};

This supports implicit conversions from int to Integer via the ctor, and
from Integer to int via the cast operator. In your case, it sounds like
the conversion may easily be more complex, but from the sound of things,
I'm guessing you already know about how to get the data from one to the
other so I'll leave that alone for now (besides, it probably wouldn't be
topical here anyway).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top