Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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 GlitchCog   Members   -  Reputation: 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.

Sponsor:

#2 Dancin_Fool   Members   -  Reputation: 621

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 GlitchCog   Members   -  Reputation: 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 Xycaleth   Members   -  Reputation: 1300

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 thedodgeruk   Members   -  Reputation: 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 scgames   Members   -  Reputation: 1977

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 phantom   Moderators   -  Reputation: 7591

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.



PARTNERS