C ++: Are structures the same as Classes?

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; // usage of someStructVariable ... } 

and this:

 void SomeFunction() { int field1; char field2; double field3; bool field4; // usage of 4 variables ... } 

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; // usage of someClassVariable ... } 

and this:

 void SomeFunction() { int field1; char field2 double field3; bool field4; // usage of 4 variables ... } 

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.

+4
source share
4 answers

There is no difference between structures and classes, except for the default value for protection (note that the default type of protection for base classes is also different). Books and my experience of more than 20 years speak of this.

Relatively empty ctor / dector by default. The standard does not ask for it. However, some compiler can generate this empty ctor / dector pair. Every intelligent optimizer immediately threw them away. If in some place a function is called that does nothing, how can you detect it? How can this affect anything other than consuming processor cycles?

MSVC does not create useless functions. It makes sense to think that every good compiler will do the same.

Regarding the examples

 struct SomeStruct { int field1; char field2; double field3; bool field4; }; void SomeFunction() { int field1; char field2; double field3; bool field4; ... } 

Filling rules, memory order, etc. can and will most likely be completely different. The optimizer can easily throw away an unused local variable. It is much less likely (if possible at all) that the optimizer will remove the data field from the structure. For this, the structure must be defined in the cpp file, certain flags must be set, etc.

I am not sure if you will find any documents on filling local vars on the stack. AFAIK, it is 100% up to the compiler to create this layout. On the contrary, the layout of structures / classes is described, there are #pargma and command line keys that control this, etc.

+1
source

I believe that the author of this article is wrong. Although there is probably no difference between the version of the structure and the non-member layout variable of the two functions, I do not think this is guaranteed. The only thing I can think of is to ensure that since it is a POD, the address of the structure and the first element is the same ... and each member follows in memory after that at some point.

In any case, since POD (and classes may be too much, do not make a THAT error), the data will be initialized.

In any case, I would recommend not to make such an assumption. If you wrote code that used it, and I cannot imagine why you want it, most other developers will still find this incomprehensible. Express law books only if you need to. Otherwise, prefer the code in the manner that people are used to. The only important part of all of this is that you really should keep in mind that POD objects are not initialized unless you do this explicitly.

+2
source

The only difference is that the members of the structures are public by default, and the members of the classes are private by default (when I say by default, I mean "unless otherwise specified"). Check this code:

 #include <iostream> using namespace std; struct A { int x; int y; }; class A obj1; int main() { obj1.x = 0; obj1.y = 1; cout << obj1.x << " " << obj1.y << endl; return 0; } 

The code compiles and works just fine.

+2
source

are the same in terms of machine instructions?

There is no reason not to be. But there is no guarantee from the standard.

What is no call to someClass constructor?

Yes there is a call to the constructor. But the constructor does not work (since all POD members and the way to declare someClass someClassVariable; causes initialization initialization, which does nothing for POD members). Since there is no work to be done, there is no need to set any instructions.

Or is the allocated memory the same as instantiating a class or defining variables separately?

A class may contain a complement that declares variables separately.
I'm also sure that it will be easier for the compiler to optimize individual variables.

What about the supplement?

Yes, there is the possibility of filling in the structure (struct / class).

structures and classes fill. Will the filling in these cases be the same?

Yes. Just make sure you compare apples to apples (i.e.)

 struct SomeStruct { int field1; char field2; double field3; bool field4; }; class SomeStruct { public: /// Make sure you add this line. Now they are identical. int field1; char field2; double field3; bool field4; }; 
+1
source

Source: https://habr.com/ru/post/1438338/


All Articles