Implement the package of Struct internal data in C

xiaoxiao2021-03-06  17

The encapsulation is an important part of the object-oriented program for ensuring the robustness of the program. Part of the encapsulation is to encapsulate object internal data, ie, the external program is not allowed to directly reference objects, but through the corresponding GET / SET method to attribute access. There are many advantages in the encapsulation: on the one hand, the robustness of the program is improved, preventing external programs from intending to set the attributes into illegal values; on the other hand, the flexibility of the program is improved, such as the GET / SET method is not necessarily the corresponding one. Data members, the internal implementation of a class can make certain programs modifications and affect the external interface. In object-oriented language-oriented languages ​​such as C and Java, the purpose of encapsulation of the object's internal data is achieved by access control declaration of class members (public, private, etc.).

In C, the Struct structure is corresponding to the object (let's say it). General C procedures will be provided to other program access to the structure definition in .h file, such as the structure to define a DATE_T representation of the date type and provide some operations for the DATE_T type, such as the DATE_DIFF function is used to calculate the difference between the two days. The general practices are as follows:

Date.htyped; intorm; int day;} DATE_T; / * Calculate the difference between two dates, returning between two days * / int Date_diff (Date_t * D1, Date_t * D2);

What is done is that the general other programs include Date.h, you can directly access and set the properties of the DATE_T structure, but this may bring some problems, such as the following code segment:

Date_t d; d.Year = 1900; D.Month = 2; D.day = 29;

The date was brought to February 29, 1900, and the date actually on February 29, 1900 was illegal, as in 1900 is not a leap year. Therefore, one but the programmer designed to design the DATE_T structure gives the internal data of the DATE_T structure to its user, he cannot guarantee that a DATE_T structure always represents a correct date, and this is usually very important. For example, the provider of the DATE_T structure also provides a series of operations for the DATE_T structure (which is very common), such as DATE_DIFF functions between the two dates, etc., if these operations accept illegal parameters, it may output strange The result is even unable to work.

One way to solve this problem will define the DATE_T structure in the .c file that implements the DATE_T operation, and only the handle of the DATE object (actually a pointer to the DATE_T object), as shown below:

Date.htypedef void * handle_t;

/ * Calculate the difference between two dates, return the number of days between the two days * / int DIFF (Handle_t D1, Handle_T D2);

Thus, since the caller does not know the internal structure of the DATE type, it is impossible to use the above "violent" means to create an illegal date. Of course, the realizes of the date type also provides some operations to create a DATE_T object to get the date of the date, month, day and other components.

But this method also brings new problems. Assuming the time type is also provided, but it is defined as Handle_T in the external .h file, so that if the caller is processed:

Handle_t D = Date_create (Year, Month, Day); Handle_t T = Time_create (Hour, Minute, Second); INT DIFF = DATE_DIFF (D, T);

The caller calls a date_diff function as a date type and a time type as a parameter, which is obviously wrong, but the problem is that the date and time type is defined as handle_t, so the compiler can't discover this type of error, the program is running It is not necessarily discovered immediately. Therefore, the above practice is prone to errors that are mixed with various types of objects due to completely obliterate the differences between various types. Another better way is to define the following definition in .H file:

Date.htypedef struct date date_t;

Time.htypedEf struct time Time_t;

The Date, TIME structure is also not directly defined in Date.h and Time.h. However, at this time, the caller is not easy to generate the incorrect type, such as the following code, the compiler will prompt the type without match:

Date_t * d = DATE_CREATE (YEAR, MONTH, DAY); TIME_T * T = Time_create (Hour, Minute, Second); INT DIFF = DATE_DIFF (D, T);

转载请注明原文地址:https://www.9cbs.com/read-45480.html

New Post(0)