Big talk function pointer and enumeration this pair of pointer enumeration, pointer Enumeration

Source: Internet
Author: User

Big talk function pointer and enumeration this pair of pointer enumeration, pointer Enumeration

I. Cause

(1)A function pointer is a pointer variable pointing to a function. It is essentially a pointer variable and a pointer pointing to the first address of a function (possibly a code area). As we all know, the array name is a constant pointer to the first element of the array,For a function, the function name is also a constant pointer to the first instruction of the function. Callback functions and enumeration

(2) The callback function is an advanced application of function pointers in C language,A callback function is a function called by a function pointer. If you pass the function pointer (the function entry address) to another function, when this function pointer is used to call the function to which it points, we will say that this function is a callback function. Big talk function pointer and pointer Function

(2-2) Why use enumeration?A week has only seven days, and a year has only twelve months. It is obviously inappropriate to describe these values as integer, dense, or other types.Therefore, the programming language provides an enumeration type (also called enumeration ).

(3) The declaration of enumeration, in the form of: (the enumeration name is equivalent to the name of this set, which can be dispensable; The type definition ends with a semicolon (;). The value of each member must not exceed the defined range.

Access modifier enum enumeration name: basic type {enumeration member (identifier )}

(4)Each enumerated member has an associated constant value. The value type is the basic type of enumeration.. The enumeration type belongs to the sequence type. In this way, each enumeration member represents a value (such as int bool), which can be used as the downloading value of the Function Array. (The enumerated member type can be any integer except char)

Ii. Detailed explanation

1) Enumeration membersIt cannot be a numerical constant, a character constant, or a String constant. It cannot contain single or double quotation marks.. For example, the following definition is incorrect:

Enum Days {'sun', 'mon', 'tues ', 'wed', 'thu', 'fri', 'sat '}

Enum Days {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat "}

2)Each enumerated member has an associated constant value. The value type is the basic type of enumeration.. The enumeration type belongs to the sequence type. Determine the sequence of enumeration members according to the order of enumeration members when defining types,For example, if the default basic type is int, the serial number starts from 0, and the values of each enumeration member increase by 1 in sequence.Of course, you can assign values explicitly. For example:

Enum Days {Sunday = 2, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

The enumerated member is a constant, not a variable. Even if it is an identifier, it cannot be usedAssign a value to a variable.

3)The declaration of enumeration, in the form of: (the enumeration name is equivalent to the name of this set, which can be dispensable; The type definition ends with a semicolon (;). The value of each member must not exceed the defined range.

Access modifier enum enumeration name: basic type {enumeration member (identifier )}
Note: Any Enumeration type has a basic type, which can be any integer except char, such as byte, sbyte, short, ushort, int, uint, long, And ulong. The basic type must be able to represent all enumeration members defined in this enumeration. You can explicitly declare the basic type during enumeration declaration. When no explicit declaration is made, the default basic type is int. The base type specifies the memory size allocated to each enumerative Number member.

4)The variables a, B, and c are described as the preceding Days. You can use either of the following methods: each adjacent enumeration member is separated by commas (,). The last one has no commas.Enumeration classes are often used in the switch-case structure. The common (Enum) methods mainly include the following:

Iii. Application Instances

(1) For example: students are classified into college students, middle school students, and junior high school students.

Enum student {colleage, high, middle, junior}

// James is a college student

Student xiaoming = colleage;

// Judge whether Xiaogang is a high school student

If (xiaogang = high)

(2) The enumeration element itself is defined by the system as a numerical value indicating the serial number. (The enumeration element is a numerical value.) Starting from 0, the order is defined as 0, 1, 2 .... For example, in weekday, sun is 0, mon is 1 ,..., The sat value is 6.

Int main ()

{

Enum weekday {sun, mon, tue, wed, thu, fri, sat} a, B, c;

A = sun; B = mon;

C = tue;

Printf ("% d, % d, % d", a, B, c );

}

Note:

Only enumeration values can be assigned to enumeration variables, and the element values cannot be directly assigned to enumeration variables. For example:

A = sum; B = mon; is correct.

A = 0; B = 1; is incorrect.

If you want to assign a value to an enumerated variable, you must use forced type conversion. For example:

A = (enum weekday) 2;

In this way, the enumerated element with the ordinal number 2 is assigned to the enumerated variable a, which is equivalent:

A = tue;

It should also be noted that enumeration elements are neither character constants nor string constants. Do not add single or double quotation marks when using them. The enumerated member is a constant, not a variable. Even if it is an identifier, it cannot be usedAssign a value to a variable.

Int main () {enum body {a, B, c, d} month [31], j; int I; j = a; for (I = 1; I <= 30; I ++) {month [I] = j; j ++; if (j> d) j =; // It can be seen that the enumeration element is equivalent to a constant defined as a fixed value} for (I = 1; I <= 30; I ++) {switch (month [I]) {case a: printf ("% 2d % c \ t", I, 'A'); break; case B: printf ("% 2d % c \ t", I, 'B'); break; case c: printf ("% 2d % c \ t", I, 'C'); break; case d: printf ("% 2d % c \ t", I, 'D'); break; default: break;} printf ("\ n ");}

(3) The complete example can be compared with the callback function and enumeration to find the following advantages:

# Include <iostream> using namespace std; double add (double, double); double sub (double, double); double mul (double, double); double div (double, double); // or other pointer double (* oper_func []) (double, double) = {add, sub, mul, div}; double calculate (double, double, double (* func) (double, double); enum operations {add _ = 0, sub _ = 1, mul _ = 2, div _ = 3}; int main () {operations operation = sub _; cout <operation <endl; Double op1, op2; cin> op1> op2; int index = 0; // index can be changed as needed // double result = oper_func [index] (op1, op2); // double result = calculate (op1, op2, oper_func [sub _]); // In the real project, the problem is solved based on the actual string name, // you do not need to consider whether the value corresponding to the string will cross the border or not, because the enumerated members are fixed within a certain value range. Cout <"result =" <result <endl; return (0);} double calculate (double op1, double op2, double (* func) (double, double )) {return func (op1, op2);} double add (double op1, double op2) {return op1 + op2;} double sub (double op1, double op2) {return op1-op2;} double mul (double op1, double op2) {return op1 * op2;} double div (double op1, double op2) {if (op2! = 0) return op1/op2; return-1; // Failed}
Explanation: in this way, you can use the enum operators and function pointer array (double (* oper_func []) (double, double) = {}), and the callback function (double calculate (double op1, double op2, double (* func) (double, double) replaces the complex swich-case sentence pattern.

double calculate(double op1, double op2, double (*func)(double,double)){    return func(op1,op2);}
Of course, the current Code still needs to be improved. For example, in the calculation step, only data of the double type can be calculated. Therefore, optimization is required to calculate data of any type.

(4) insertion error Explanation Method undefined reference

Recently, programming in Linux finds a strange phenomenon, that is, errors are always reported when a static library is linked, similar to the following errors:

~~~~~~~ Undefined reference to 'func'

This is the most typical undefined reference error, because the implementation file of a function cannot be found at the link time. (one sentence is that the implementation part of the function is missing, but the Declaration part is not enough)

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.