By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Casting to a 'different' class...

P: n/a
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...
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
nl wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
nl wrote:
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.

--
Unforgiven

Jul 22 '05 #3

P: n/a
Unforgiven wrote:
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*

--
Unforgiven
Jul 22 '05 #4

P: n/a
Unforgiven wrote:
Unforgiven wrote:
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*


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.

--
Unforgiven

Jul 22 '05 #5

P: n/a
"Unforgiven" <ja*******@hotmail.com> wrote:
Unforgiven wrote:
Unforgiven wrote:
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*


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.

--
Unforgiven


You are forgiven ! :-)

David F
Jul 22 '05 #6

P: n/a
David Fisher wrote:
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. ^_^

--
Unforgiven

Jul 22 '05 #7

P: n/a
Unforgiven wrote:
David Fisher wrote:
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. ^_^


You are forgiven!

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

Jul 22 '05 #8

P: n/a
In article <br*********@news3.tilbu1.nb.home.nl>, no*@listed.com says...

[ ... ]
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).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.