Struct versus class?

Started by
45 comments, last by Nish Wk 10 years, 3 months ago

I would also like to add that the compiler adds the operator= for any struct, by coping 1-1 all its fields, in a class you generally have to define your own class_name operator=(const class_name right); however there are certain conditions on which the C++ compiler may add that operator automatically (implicitly). If I recall correctly there is yet another difference, but I fail to remember it for the time being, my apologies!

Hope I helped a little

Advertisement

I would also like to add that the compiler adds the operator= for any struct, by coping 1-1 all its fields, in a class you generally have to define your own class_name operator=(const class_name right); however there are certain conditions on which the C++ compiler may add that operator automatically (implicitly). If I recall correctly there is yet another difference, but I fail to remember it for the time being, my apologies!

Hope I helped a little

No you dont default copy constructor, default construct/destructor and default assignment operator are generated for a class, as soon as you write one of these the compiler will not generate a default one. There really is no difference between a class or a struct beyond the default access modifiers.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

The other slight difference is the default inheritance type, if you omit the specifier when you inherit, but very few people ever seem to do that, or even use private inheritance much so it isn't a big deal.

class x : y { }; // default private inheritance

struct x : y { }; // default public inheritance

This and the default public/private access within the class are categorically the ONLY differences between struct and class as far as the compiler is concerned. However, it is a reasonably common convention to use structs to represent all-public, data-only types and classes for everything else.

Struct is really only included in C++ to support backward compatibility with old C code.

structs have an advantage over class of being properly serializable. As soon as your object definition does not contain an advanced datatype pointer, you are fully valid to write it as a struct, which fits for your vector class. But this all is just a question of code producing culture, nothing critical.

But as to vectors, I would recomend you to not write algebraic functions as members of a vector, but go for a globaly used Algebra object that takes vectors as inputs and returns, or, manipulates existing vectors for result.

structs have an advantage over class of being properly serializable.


This is completely false. Making something a struct does not automatically make somethings serializable by default. Making something a class does not automatically make is unserializable. In C++, the difference between class and struct is completely cosmetic: the default visibility and inheritance for a struct is public, private for a class.

See std::is_pod and related standard library constructs for questions regarding serializability.

This is completely false. Making something a struct does not automatically make somethings serializable by default. Making something a class does not automatically make is unserializable. In C++, the difference between class and struct is completely cosmetic

I did not say struct is serializable as an implicit fact. I said what it should be in my eyes - serializable. A single struct object, fully operative, can be resurected from its memory foot print. I repeat, not a fact, but an -expectation? Get me? At least this is my vision of a struct, if someone tends to write a struct with function members and all that. I do not expect a struct like this

struct vec2d

{

float x;

float y;

CAlg* m_pAlg;

void Init()

{

x=m_pAlg.Nod(x,y);

}

}

You have mentioned that struct/class difference in compiler interpretation is none. It is pure interpratation of a programmer, meaning, a programer should consider struct as a certain manifesting, towards a memory available object, as is. Interpratation and purpose of struct in area of profesional instruction writing still exists! You can(should) reproduce a struct from its own allocated memory- as a self encapsulated object on self memory (what is a definition of a "program" actualy as well). While a class like CSceneManager, is a wide memory object, not reproducible and dependant only on its allocated memory. A struct object is reproducible and fully operatible on its member functions, or outer functions expecting this object, from its allocated memory. From a programmer professioal view, if someone codes struct that depend on outer memory, is an unprofessional programmer who does not use instruction writing properly. You of course does not have to write a struct to achieve this, but if you encourage yourself to define object as a struct, be aware of not making yourself look like a noob.

You have mentioned that struct/class difference in compiler interpretation is none. It is pure interpratation of a programmer, meaning, a programer should consider struct as a certain manifesting, towards a memory available object, as is. Interpratation and purpose of struct in area of profesional instruction writing still exists! You can(should) reproduce a struct from its own allocated memory- as a self encapsulated object on self memory (what is a definition of a "program" actualy as well). While a class like CSceneManager, is a wide memory object, not reproducible and dependant only on its allocated memory. A struct object is reproducible and fully operatible on its member functions, or outer functions expecting this object, from its allocated memory. From a programmer professioal view, if someone codes struct that depend on outer memory, is an unprofessional programmer who does not use instruction writing properly. You of course does not have to write a struct to achieve this, but if you encourage yourself to define object as a struct, be aware of not making yourself look like a noob.

I have never come across this expectation among professionals. Not really sure where you are getting it from.

struct keyword exists in C++ purely to allow backward compatibility with older code. POD is a very different thing to struct as has been pointed out and the fact that something is implemented as a struct should in no way be taken as an indication that it is possible to serialize/restore it from its memory footprint.

What about a struct containing a pointer to memory it doesn't own, such as a node in a linked list in C? That can't be serialized and recreated from its memory footprint. Does that mean it is noobish? Strange idea.

Thanks for all the replies and help.
For now I'm convinced that for vector3 I'll keep the struct and for functions that don't actually change or return "this" make free functions within the same header/cpp files for keeping code maintainable. Basically that means only operator functions within the struct, and maybe normalize since it directly changes the members.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

One thing that I think that Johnny is trying to say is that it's a common convention for struct to be used for POD types, and class to be used for non-POD types.

This topic is closed to new replies.

Advertisement