Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with D3DXVECTOR2 pointers

Started by Mr Teatime May 3, 2003 at 11:40 AM 3 replies 700+ views
Original Post
Mr Teatime
Mr Teatime
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.
IFooBar
IFooBar
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 ] ::::
[size=2]aliak.net
Mr Teatime
Mr Teatime
Ahh, I completely forgot about the arrow operator, oops.

Thanks for the help.
okonomiyaki
okonomiyaki
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?
Illumini
Illumini
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.