Struct versus class?

Started by
45 comments, last by Nish Wk 10 years, 2 months ago
It should be noted that even assuming structs for POD/serializable constructs were a general convention (as this thread has shown, that's not the case), it would be simply a convention, not something enforced by the compiler.

C++11 offers compile-time checking for exactly this problem though (for example in a static assert). For serializability (excluding padding-related problems) std::is_trivially_copyable is the perfect fit. The whole std::is_pod constraint is an unnecessarily strong requirement for just serialization issues.
Advertisement

Maybe a bit off topic, but I don't have a clue what you mean in the last reply.

Can you explain it a bit more 'dummy', serializable constructs, convention, serializability, padding, serialization issues

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

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

Serializable (in this context) means you can just write the struct directly to disk with fwrite in binary mode and read it back in with fread,

Structs are usually padded so if you have a 16bit short followed by a 32bit int directly after each other the 32bit int normally has an address 4 bytes after the address of the 16bit short.

A convention is a place where people put on bad costumes and try and meet actors from Star Trek.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

oh not this again, been answering this question since forever lol.

The only difference is in struct by default(in absence of access modifier) all members are public and where as in class they are private that makes class a better candidate for implementing abstraction in genera,l however with explicit access modifier inside a struct makes it same thing as class in every which way.

Any other difference one can rightly think of is either derived from or result of above ^.

oh not this again, been answering this question since forever lol.

The only difference is in struct by default(in absence of access modifier) all members are private and where as in class they are public that makes class a better candidate for implementing abstraction in genera,l however with explicit access modifier inside a struct makes it same thing as class in every which way.

Any other difference one can rightly think of is either derived from or result of above ^.

Aside from the fact that you got your default access specifies wrong; class is private and struct is public and not the other way around, this thread was created to ask the question where people draw the line between when to use a class and when to use a struct. Some people use class or struct for everything, some people use structs for storage types only, and so on. So no, this is not your typical struct vs. class question.

The fact that 10 people in this thread have provided 20 different answers makes it clear that the line to draw is fuzzy. It's simply a matter of personal preference. Frob's reasoning in the second post in this thread makes the most sense to me,

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

. So no, this is not your typical struct vs. class question.

Oight Brother Bob, answered too hastily.

This topic is closed to new replies.

Advertisement