struct and class !!!
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; }
};
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.
As for your question: the benefit is accessing the struct members directly. Just don''t go too far with the public thing.
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. -
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. -
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement