Re: What's with "typedef interface ID3DXLine ID3DXLine;" in d3dx9core.h?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<res7cxbi@xxxxxxxxxxx> wrote in message news:1151641813.536950.179820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
What is the purpose of "typedef interface ID3DXLine ID3DXLine;" in
d3dx9core.h?

im just curious, why does this header file have this piece of code at
line 681? isn't just defining itself again? all the other interfaces in
the header have something like this

thanks in advance

This is really more of a c++ question than DirectX specific, but I'll take a stab at it.

The typedef serves as a forward declaration, that is, it specifies the existence of such an interface before it is actually defined. This allows the line that follows it, which defines a type LPD3DXLINE as a pointer to that interface, even though the interface class has not been defined yet. Another example of a forward declaration without a typedef, that might be more apparent goes something like this:

class B;

class A {
...
B * m_pB;
}
class B {
...
A * m_pA;
}

Without the forward declaration "class B;" there would be a kind of "which came first, the chicken or the egg" problem, because the compiler would not know of the existance of the B class when it attempts to compile the A class and encounters a pointer to a type "B". If class B didn't have a pointer to an A instance you could just place the declaration of class B prior to that of class A, but since it does the same problem would arise compiling the B class. The forward declaration lets the compiler know that a declaration of a class type B will be forthcoming, establishing the type identifier before it is needed in the declaration of class A.

In the case of LPD3DXLINE, that typedef could be made after ID3DXLine has been defined. However, this would mean that you could not make use of that pointer type within the class declaration. While they don't make use of it in this case, the forward declaration would allow the LPD3DXLINE type to be used in the declaration of ID3DXLine, such as in the declaration of a member variable, or as a parameter or return value for a member function. So while none of the classes in d3dx9core.h make use of these pointer types within declaration of the same class, it is likely implemented for the sake of consistency.


--
Robert Dunlop
The X-Zone
http://www.directxzone.com/
Microsoft DirectX MVP
-------------
The opinions expressed in this message are my own personal views and do not reflect the official views of the Microsoft Corporation.
The MVP program does not constitute employment or contractual obligation with Microsoft.

.



Relevant Pages

  • Re: object system...
    ... type declaration and pulling the latter with it. ... produces the result, the type or interface. ... you prevent the compiler to do it for you. ... And with most of optimizations the ...
    (comp.object)
  • Build regressions/improvements in v2.6.36-rc3
    ... declaration of function 'iowrite8': => 46 ... of 'memcpy' discards qualifiers from pointer target type: ... pointer types lacks a cast: ... dynamic stack allocation: => 392 ...
    (Linux-Kernel)
  • Re: gcc knows about malloc()
    ... and may prevent the compiler from ... void *malloc; ... different effect than a different piece of C syntax in a declaration. ... and converting the bogus pointer to a pointer to function ...
    (comp.lang.c)
  • Build regressions/improvements in v2.6.36-rc2
    ... of 'memcpy' discards qualifiers from pointer target type: ... pointer from integer without a cast: ... fs/eventpoll.c: warning: 'pwq' may be used uninitialized in this ... or declaration, which is probably not what you want: ...
    (Linux-Kernel)
  • Re: including cray pointers in fortran modules
    ...  pointer (ipt, x) ... The first layer is with the declaration of ipt and that PROGRAM ... Cray pointer at has 4 bytes of precision; ...   ...
    (comp.lang.fortran)