Possible duplicate:
What are the differences between structure and class in C ++
This question has been asked and answered a lot, but from time to time I come across something confusing.
To summarize, the difference between C ++ structures and classes is well-known public access versus private access. In addition, the C ++ compiler considers the structure in the same way as for a class. Structures can have constructors, copy constructors, virtual functions. etc. And the memory layout of the structure is the same declaration as for the class. And the reason C ++ has structures for backward compatibility with C.
Now that people are confused about which one to use, struct or class, the rule of thumb is that if you only have old data, use struct. Otherwise, use a class. And I read that structures are good at serializing, but not where it comes from.
Then the other day I came across this article: http://www.codeproject.com/Articles/468882/Introduction-to-a-Cplusplus-low-level-object-model
It says that if we have (directly quote):
struct SomeStruct { int field1; char field2; double field3; bool field4; };
that is:
void SomeFunction() { SomeStruct someStructVariable;
and this:
void SomeFunction() { int field1; char field2; double field3; bool field4;
match up.
He says that the generated machine code is the same if we have a structure or just write the variables inside the function. Now, of course, this only applies to your structure, if POD.
That's where I got confused. In Effective C ++, Scott Meyers says there is no such thing as an empty class.
If we have:
class EmptyClass { };
It is actually laid out by the compiler, for example:
class EmptyClass { EmptyClass() {} ~EmptyClass() {} ... };
So you will not have an empty class.
Now, if we change the above structure to a class:
class SomeClass { int field1; char field2 double field3; bool field4; };
Does this mean that:
void SomeFunction() { someClass someClassVariable;
and this:
void SomeFunction() { int field1; char field2 double field3; bool field4;
identical in terms of machine instructions? What is no call to someClass constructor? Or is the allocated memory the same as instantiating a class or defining variables individually? What about the add-ons? structures and classes complement. Will the filling in these cases be the same?
I would really appreciate it if someone could shed some light on this.