Structure Knowledge Summary

Source: Internet
Author: User

Source: http://blog.csdn.net/gnuhpc/archive/2009/06/28/4304124.aspx

 

I. struct and function parameters
Struct function parameters can be divided into passing values and passing pointers.
1. when a value is passed, the struct parameter will be copied. Modifying the value of the struct parameter member in the function body is actually modifying the value of a temporary copy Member of the call parameter, which does not affect the call parameter. In this case, because the copy of struct parameters is involved, the space and time efficiency of the program are affected, so this method is basically not required.
For example:
Typedef struct tagstudent {
Char name [20];
Int age;
} Student;

Void fun (student Stu)
{
Printf ("Stu. Name = % s, Stu. Age = % d/N", Stu. Name, Stu. Age );
}

2. When the pointer is passed, the first address of the struct is directly transferred to the function body. In the function body, the pointer is used to reference the struct Member, which can have a real impact on the value of the struct parameter member. This method is highly efficient and often used.
For example:
Typedef struct tagstudent {
Char name [20];
Int age;
} Student;

Void fun (student * pstu)
{
Printf ("pstu-> name = % s, pstu-> age = % d/N", pstu-> name, pstu-> age );
}

Ii. struct and function return values
For some versions of C language compilers, the returned values can only be basic data types such as int, Char, and pointer. Therefore, struct, as a combination data type, cannot be returned as values, in some versions of C compilers, struct variables can be directly returned, and struct variables can be directly returned in C ++.

Example of directly returning struct variables;
Typedef struct tagstudent {
Char name [20];
Int age;
} Student;

Student fun ();
Int _ tmain (INT argc, _ tchar * argv [])
{
Student P = fun ();
Printf ("P. Name = % s", p. Name );
Return 0;
}

Student fun ()
{
Student Stu;
Stu. Age = 18;
Strcpy (STU. Name, "Xiaoming ");
Return Stu;
}

An example of returning a struct using a pointer is as follows:
Typedef struct tagstudent {
Char name [20];
Int age;
} Student;

Student * Fun ()
{
Student * P = malloc (sizeof (student ));
P-> age = 18;
Strcpy (p-> name, "Xiaoming ");
Return P;
}

2. Comparison of struct and class
Differences:
Structure is a type of custom data declared with the keyword struct. Similar to a class, it can also contain constructors, constants, fields, methods, attributes, indexers, operators, and nested types. However, the structure is a value type.
1. the constructor of the structure is different from the constructor of the class.
A. the structure cannot contain explicit non-parameter constructors. Structure members are automatically initialized as their default values.
B. The structure cannot contain the following initialization classes: Base (argument-list );
2. For instance field members in the structure, the value assignment cannot be initialized during the declaration.
3. After declaring the structure type, you can use the new operator to create a constructor object or use the new keyword. If new is not used, the field remains unassigned and the object is unavailable before all fields are initialized.
4. The structure does not support inheritance, that is, a structure cannot inherit from another structure or class, and cannot be used as the base class of a class. However, the structure inherits from the base class object. Structure can also implement interfaces.

5. When should I use the structure? The structure is simple and useful, but remember that the structure created in the stack is a value type, and the class is a reference type. Whenever a frequently used type is required, and in most cases this type is only some data, the structure can achieve better performance than the class.
Finally, let's quote "Crossing the forest" in the blog Garden:
Structure is a value type, which affects performance. However, according to the structure, this impact may be positive or negative. The positive effect is that when memory is allocated to the structure, the speed is very fast because they join or save the memory in the stack. When the structure is deleted beyond the scope, the speed is also very fast. On the other hand, as long as the structure is passed as a parameter or a structure is assigned to another structure (for example, a = B, where A and B are structures), all the content of the structure will be copied, for classes, only references are copied. In this way, there will be performance loss, depending on the size of the structure, the performance loss is also different. Note that the structure is mainly used for small data structures. But when the structure is passed to the method as a parameter, it should be passed as the ref parameter to avoid performance loss-at this time, only the address of the structure in the memory is passed, in this way, the transfer speed is as fast as the transfer speed in the class. In this case, you must note that the called method can change the value of the structure.
Another point: struct and class should be essentially the same, but the default access permissions are different (struct is public by default, and class is private by default ). A large part of the reason for retaining struct is to be compatible with C (I think there is still some truth in this article in C ++ ). the class should be an extension of struct. In addition to setting the access type of Members, it can also have its own member functions, which can inherit and derive, as if struct can do all the class operations, but in turn, it will not work. If there is no requirement to protect data, I will use struct to do what struct can do, which is relatively simple.

Differences between classes and struct
1. The class and structure are very similar. Technically, the class is a reference, while the structure is a numerical value.
Class contains actions, methods, and members, which are the combination of organisms, while structure is a living organism,
2. For a general understanding, the class contains structure. There are square Farah in the class, member pulling, what,
While structure only has data,

Examples of classes and structures:

Structure example:
Public struct person
{
String name;
Int height;
Int weight
Public bool overweight ()
{
// Implement something
}
}
Class Example:
Public class testtime
{
Int hours;
Int minutes;
Int seconds;
Public void passtime ()
{
// Implementation of behavior
}
}

Call process:
Public class test
{
Public static Ovid main
{
Person myperson = new person // declaration Structure
Testtime mytime = new testtime // Declaration class
}
}
The difference between struct and class. In addition, the methods for defining new structures and new classes are also very similar. What are the specific differences between classes and structures?

Differences between classes and structures 
1. Value Type and reference type
Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to system. int32 structure. String corresponds to system. string Structure. You can create more value types by using the structure.
Class is a reference type: the reference type is allocated on the stack.
The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.
Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference.
Note:
1. Although the structure is different from the class type, their base types are all objects. in C #, all types of base types are objects.
2. although the new operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable.
2. Inheritance
Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed.
Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.
Note: although the structure cannot be inherited, the structure can inherit interfaces, and methods are the same as class inheritance interfaces.
Example: structure implementation Interface
Interface iimage
{
Void paint ();
}
Struct picture: iimage
{
Public void paint ()
{
// Painting code goes here
}
Private int x, y, z; // other struct members
}
3. Internal Structure:
Structure:
There is no default constructor, but you can add constructor without destructor without abstract and sealed (because it cannot be inherited) the protected modifier is not available. It is incorrect to initialize the instance field in the structure without using new initialization.
Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
New must be used for initialization.

How to Select structure or Class 
After discussing the similarities and differences between structures and classes, we will discuss how to choose a structure or a class:
1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
2. structure indicates lightweight objects such as dots, rectangles, and colors. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. In this case, the structure cost is low.
3. Classes are the best choice for presentation of abstract and multi-level object Layers
4. In most cases, this type is the best choice for structure when it is only some data.
1: The class can be inherited, and the structure cannot.
2: The class is a reference type, and the structure is a value type.
3: The class is in the heap, and the structure allocates memory on the stack.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.