Jump to content
  • Advertisement

Archived

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

SCRUB

struct and class !!!

This topic is 6133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A quick question if you will. Ive often seen structs used exactly like classes , i.e. with Constructors and Destructors etc. I always see it for Vector code. Why would you do this ? is their some benift that im missing ? for example ? struct vector3d { float X, Y, Z; inline vector3d( void ) {} inline vector3d( const float x, const float y, const float z ) { X = x; Y = y; Z = z; } inline vector3d operator + ( const vector3d& A ) const { return vector3d( X + A.X, Y + A.Y, Z + A.Z ); } inline vector3d operator + ( const float A ) const { return vector3d( X + A, Y + A, Z + A ); } inline float Dot( const vector3d& A ) const { return A.X*X + A.Y*Y + A.Z*Z; } };

Share this post


Link to post
Share on other sites
Advertisement
In structs the emphasis is on data. C++ lets you associate code with structs because you may have some initialization or termination to run during its lifetime.

As for your question: the benefit is accessing the struct members directly. Just don''t go too far with the public thing.

Share this post


Link to post
Share on other sites
In C++ the only difference between a struct and a class is that the default access-level of members is "public" for a struct and "private" for a class. That''s it.
In C, of course, you don''t have classes, and structs can only have data in them, and no methods.

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!