Jump to content

  • Log In with Google      Sign In   
  • Create Account

Structs or Classes to Represent Coordinates


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
6 replies to this topic

#1   Members   

122
Like
0Likes
Like

Posted 31 May 2011 - 01:29 PM

I'd be interested in hearing anyone's opinion on the pros and cons of representing coordinates (or more generally any vector) either as a struct or as a class in C++. It's such a fundamental data type for game programming, so I thought some people might have ideas or experiences they could share regarding having used one or the other and what difference it made in a game engine for ease of coding and/or performance. Is there any consensus on whether one is definitively better, or does it just boil down to personal preference?

typedef struct vector2D
{
	float x;
	float y;
} vector2D_t;

// OR 

class Vector2D
{
private:
public:
	Vector2D(float x, float y);
	float x;
	float y;
};

Thanks.

#2   Members   

785
Like
0Likes
Like

Posted 31 May 2011 - 01:42 PM

In c++ they're the exact same construct. A struct is just a class with everything public by default.

#3   Members   

122
Like
0Likes
Like

Posted 31 May 2011 - 01:45 PM

Ah, I see. Even down to the member functions apparently, which I didn't know structs could also have.

Question withdrawn. Sorry.

#4   Members   

2222
Like
1Likes
Like

Posted 31 May 2011 - 03:46 PM

Just to add, structs in C++ can be defined in the same way as classes:

struct Vector2D
{
 	...

};

So no need for the typedef.

#5   Members   

124
Like
0Likes
Like

Posted 05 June 2011 - 09:58 AM

you might be best using directx D3DXVECTOR2 for this , cause all the operators are overloaded, makeing maths so mauch easyer

#6   Members   

2082
Like
1Likes
Like

Posted 05 June 2011 - 11:19 AM

you might be best using directx D3DXVECTOR2 for this , cause all the operators are overloaded, makeing maths so mauch easyer

What if the OP isn't using DirectX/Direct3D, or is targeting platforms that don't support it?

(I didn't see anything in the OP's posts specifying what APIs are being used or what platforms are being targeted.)

#7   Members   

10708
Like
0Likes
Like

Posted 05 June 2011 - 12:31 PM

If the OP is targetting window then they would be better off using the newer XNA Math library (which is unrelated to the XNA C# library) anyway.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.