www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I wish I could inherit structs.

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
This is quite an interesting problem.

In the DirectX SDK, there is a structure called D3DXFRAME.  This simply 
holds some data members that describe a link in a 3D hierarchy.  It doesn't 
really matter what it does; all it matters is that it's a struct.

Now, here's the thing.  This struct _must_ be a struct, as the data members 
cannot be moved around or realigned.  This is because the internal workings 
of some of the SDK functions rely on the members being in certain places.

But at the same time, I need to be able to derive from this struct so I can 
add my own custom data to it.  Which is currently impossible.  The SDK was 
designed with C++ in mind, which can do this.

The worst part is that there isn't really a workaround.  I can't make the 
D3DXFRAME a class as there is no guarantee that the members will stay in the 
same order/alignment.  I can't make a parallel class that holds the extra 
data as there is no place for me to put a reference to the class instance in 
the D3DXFRAME structure.  About the only thing I _could_ do is to keep an 
associative array indexed by D3DXFRAME pointers.  And that's just nasty.

I'm.. stumped. 
May 16 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
I would suggest something like this:

template D3DXFRAME(S)
{
    LPSTR Name;
    D3DXMATRIX TransformationMatrix;
    D3DXMESHCONTAINER* pMeshContainer;
    S *pFrameSibling;
    S *pFrameFirstChild;
}

align(X) struct MyD3DXFRAME
{
    mixin D3DXFRAME!(MyD3DXFRAME);
    //your own stuff goes here....
}

Andrew.


"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:d6bhag$2ve4$1 digitaldaemon.com...
 This is quite an interesting problem.

 In the DirectX SDK, there is a structure called D3DXFRAME.  This simply 
 holds some data members that describe a link in a 3D hierarchy.  It 
 doesn't really matter what it does; all it matters is that it's a struct.

 Now, here's the thing.  This struct _must_ be a struct, as the data 
 members cannot be moved around or realigned.  This is because the internal 
 workings of some of the SDK functions rely on the members being in certain 
 places.

 But at the same time, I need to be able to derive from this struct so I 
 can add my own custom data to it.  Which is currently impossible.  The SDK 
 was designed with C++ in mind, which can do this.

 The worst part is that there isn't really a workaround.  I can't make the 
 D3DXFRAME a class as there is no guarantee that the members will stay in 
 the same order/alignment.  I can't make a parallel class that holds the 
 extra data as there is no place for me to put a reference to the class 
 instance in the D3DXFRAME structure.  About the only thing I _could_ do is 
 to keep an associative array indexed by D3DXFRAME pointers.  And that's 
 just nasty.

 I'm.. stumped.
 
May 16 2005
prev sibling next sibling parent Mike Parker <aldacron71 yahoo.com> writes:
Jarrett Billingsley wrote:
 This is quite an interesting problem.
 
 In the DirectX SDK, there is a structure called D3DXFRAME.  This simply 
 holds some data members that describe a link in a 3D hierarchy.  It doesn't 
 really matter what it does; all it matters is that it's a struct.
 
 Now, here's the thing.  This struct _must_ be a struct, as the data members 
 cannot be moved around or realigned.  This is because the internal workings 
 of some of the SDK functions rely on the members being in certain places.
 
 But at the same time, I need to be able to derive from this struct so I can 
 add my own custom data to it.  Which is currently impossible.  The SDK was 
 designed with C++ in mind, which can do this.
 
 The worst part is that there isn't really a workaround.  I can't make the 
 D3DXFRAME a class as there is no guarantee that the members will stay in the 
 same order/alignment.  I can't make a parallel class that holds the extra 
 data as there is no place for me to put a reference to the class instance in 
 the D3DXFRAME structure.  About the only thing I _could_ do is to keep an 
 associative array indexed by D3DXFRAME pointers.  And that's just nasty.
 
 I'm.. stumped. 
 
 
template mixD3DXFRAME() { float memberOne; float memberTwo; } struct D3DXFRAME { mixin mixD3DXFRAME; } struct Inherited { mixin mixD3DXFRAME; float memberThree; } Does this solve it?
May 16 2005
prev sibling next sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:d6bhag$2ve4$1 digitaldaemon.com...
 This is quite an interesting problem.

 In the DirectX SDK, there is a structure called D3DXFRAME.  This simply 
 holds some data members that describe a link in a 3D hierarchy.  It 
 doesn't really matter what it does; all it matters is that it's a struct.

 Now, here's the thing.  This struct _must_ be a struct, as the data 
 members cannot be moved around or realigned.  This is because the internal 
 workings of some of the SDK functions rely on the members being in certain 
 places.

 But at the same time, I need to be able to derive from this struct so I 
 can add my own custom data to it.  Which is currently impossible.  The SDK 
 was designed with C++ in mind, which can do this.

 The worst part is that there isn't really a workaround.  I can't make the 
 D3DXFRAME a class as there is no guarantee that the members will stay in 
 the same order/alignment.  I can't make a parallel class that holds the 
 extra data as there is no place for me to put a reference to the class 
 instance in the D3DXFRAME structure.  About the only thing I _could_ do is 
 to keep an associative array indexed by D3DXFRAME pointers.  And that's 
 just nasty.

 I'm.. stumped.
old-school "inheritance": struct D3DXFRAME { ... } struct MyDXFrame { D3DXFRAME native; int whatever; ... } when calling a "native" OS function that takes a D3DXFRAME pass it the native member.
May 16 2005
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:d6bhag$2ve4$1 digitaldaemon.com...
 I'm.. stumped.
I'll try one of these solutions until (if) a real one comes. Thanks guys :)
May 18 2005