Advertisement Jump to content


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


struct and class !!!

This topic is 6343 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
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.


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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!