Archived

This topic is now archived and is closed to further replies.

Mr Teatime

Help with D3DXVECTOR2 pointers

Recommended Posts

Looking for a little help with D3DXVECTOR2 pointers. Say I write a simple function that takes a D3DXVECTOR2 structure and fills it out, obviously the D3DXVECTOR2 parameter has to be passed in as a reference. Something like this... void ( D3DXVECTOR2 *position ) { *position.x = 320; *position.y = 240; } When I try and get this the compiler gives me an error. : error C2228: left of ''.x'' must have class/struct/union type : error C2228: left of ''.y'' must have class/struct/union type So what is the proper way to do this? Any help much appreciated, thanks.

Share this post


Link to post
Share on other sites
The "proper" way to do that would be to use brackets:-

(*position).x = 320;
(*position).y = 240;


And an even properer way would be to use the arrow operator becuase position is s pointer, so to access the members of a pointer you should use the ''->'' operator like so:

position->x = 320;
position->y = 240;


or you could just make life easier and make the function like this

void something( D3DXVECTOR& position )
{
position.x = 320;
position.y = 240;
}

that way the original variable gets altered instead of a copy and it''s cleaner then using all that pointer and dereferencing stuffage.


:::: [ Triple Buffer V2.0 ] ::::

Share this post


Link to post
Share on other sites
quote:


or you could just make life easier and make the function like this

void something( D3DXVECTOR& position )
{
position.x = 320;
position.y = 240;
}





I''ve always wondered this. What exactly is happening here? I know that the ''&'' symbol represents a reference, but then aren''t you passing in a reference, and therefore position is a pointer? How can you use the ''.'' operator then?

Share this post


Link to post
Share on other sites
Because the C++ refrence operator operates at two levels. Under the hood it is only passing an address. However to the programmer it is treated as simply an "alias" for the original variable.

[edited by - Illumini on May 4, 2003 6:29:35 PM]

Share this post


Link to post
Share on other sites