// ================================================ ====================================
// Title:
// C ++ vs C # (11): Tiny differences between struct and Object
// Author:
// Norains
// Date:
// Friday 18-January-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================
1. Tiny differences between struct and Object
Struct (struct) is a legacy of C. Since C ++ and C # Have an uncommon relationship with C, it naturally inherits this lineage. Since it is also under the banner of object-oriented, there is naturally some innovation, there is a class (class), and class instantiation has an object.
So are struct and class two different things? The answer cannot be simply "yes" or "no", because the relationship between the two is quite large. In C ++, struct and class are actually two things. The only difference is that, if a member does not have an access modifier, struct is public by default and class is private! If you use code, you may be more likely to understand it, as shown below:
// C ++ <br/> struct mystruct <br/>{< br/> // no explicit access modifier. The default value is public <br/> int value; <br/>}; </P> <p> class myclass <br/>{< br/> // no explicit access modifier, the default value is private <br/> int value; <br/>}; </P> <p> mystruct; <br/> mystruct. value = 10; </P> <p> myclass; <br/> // This assignment statement is incorrect because the value is private. <br/> myclass. value = 10; <br/>
The differences between the access modifier in struct and class do not exist in C #, because in C #, whether it is struct or class, the access modifier is private by default.
In C ++, there is another difference between struct and class: struct supports the initialization list, but the class does not, as shown in the following code:
// C ++ <br/> mystruct = {10}; </P> <p> // because class does not support initialization list, therefore, an error occurred while compiling this line of statements. <br/> myclass = {10 };
If you assign a value to the initialization list of the object, the compiler reports the error: Error c2552: 'myclass': Non-aggregates cannot be initialized with initializer list.
After talking about the many differences between C ++, but none of them correspond to C #, can we assert that the struct of C # is equivalent to class? Of course not. In C #, struct is a value type, while class is a reference type. The difference in this type directly affects the "=" value assignment operator. Let's look at a specific piece of code:
// C # <br/> struct mystruct <br/>{< br/> Public int value; <br/> }; </P> <p> class myclass <br/>{< br/> Public int value; <br/> }; </P> <p> static void main (string [] ARGs) <br/>{< br/> mystruct mystruct1 = new mystruct (); <br/> mystruct mystruct2 = mystruct1; <br/> mystruct1.value = 10; <br/> mystruct2.value = 20; <br/> console. writeline ("mystruct1.value = {0}", mystruct1.value); <br/> console. writeline ("mystruct2.value = {0}", mystruct2.value); </P> <p> myclass myclass1 = new myclass (); <br/> myclass myclass2 = myclass1; <br/> myclass1.value = 10; <br/> myclass2.value = 20; <br/> console. writeline ("myclass1.value = {0}", myclass1.value); <br/> console. writeline ("myclass2.value = {0}", myclass2.value); <br/>}< br/>
The output result of this Code section is as follows:
Mystruct1.value = 10
Mystruct2.value = 20
Myclass1.value = 20
Myclass2.value = 20
From the results, we can see that the "=" of the class is equivalent to a pointer assignment. The myclass myclass2 = myclass1 statement can be regarded as assigning the address of myclass1 to myclass2, therefore, any changes to myclass2 will be reflected in myclass1. Struct's "=" operation is a simple value assignment operation, that is, copying mystruct1.value to mystruct2.value. Any operation on mystruct2 will not affect mystruct1.
The difference between struct and Class "=" does not exist in C ++. In both struct and class, "=" in C ++ is a value assignment operation, that is, it is consistent with the struct of C.