Sign in to follow this  
derek7

D3DXVECTOR3

Recommended Posts

typedef struct { FLOAT x; FLOAT y; FLOAT z; } D3DXVECTOR3; no constructor ,but why I can use D3DXVECTOR3(...)to initialize? 2. it is a struct ,so why not use D3DXVECTOR3 D = {...}to initialize?

Share this post


Link to post
Share on other sites
There is a constructor...

The entire definition of D3DXVECTOR3

typedef struct D3DXVECTOR3 : public D3DVECTOR
{
public:
D3DXVECTOR3() {};
D3DXVECTOR3( CONST FLOAT * );
D3DXVECTOR3( CONST D3DVECTOR& );
D3DXVECTOR3( CONST D3DXFLOAT16 * );
D3DXVECTOR3( FLOAT x, FLOAT y, FLOAT z );

// casting
operator FLOAT* ();
operator CONST FLOAT* () const;

// assignment operators
D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& );
D3DXVECTOR3& operator -= ( CONST D3DXVECTOR3& );
D3DXVECTOR3& operator *= ( FLOAT );
D3DXVECTOR3& operator /= ( FLOAT );

// unary operators
D3DXVECTOR3 operator + () const;
D3DXVECTOR3 operator - () const;

// binary operators
D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;
D3DXVECTOR3 operator - ( CONST D3DXVECTOR3& ) const;
D3DXVECTOR3 operator * ( FLOAT ) const;
D3DXVECTOR3 operator / ( FLOAT ) const;

friend D3DXVECTOR3 operator * ( FLOAT, CONST struct D3DXVECTOR3& );

BOOL operator == ( CONST D3DXVECTOR3& ) const;
BOOL operator != ( CONST D3DXVECTOR3& ) const;

} D3DXVECTOR3, *LPD3DXVECTOR3;



ace

Share this post


Link to post
Share on other sites
For 1. see Ace's answer. For 2: because that only works when declaring and initializing the variable at once. You cannot use it to assign different values later on.

So with a constructor you can do:

D3DXVECTOR3 vPosition = D3DXVECTOR3(0.0f,0.0f,0.0f);
...
vPosition = D3DXVECTOR3(0.0f,1.0f,0.0f);


But with the curly bracket method you cannot do that:

D3DXVECTOR3 vPosition = { 0.0f, 0.0f, 0.0f };
...
vPosition = { 0.0f, 1.0f, 0.0f }; // Not allowed!


Also, it is healthy design. Suppose the internals of the struct are changed in the future (not likely but suppose it for the sake of argument). The curly bracket method would be invalidated while the constructor method can be continued to be supported by supplying the right constructor.

Greetz,

Illco

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this