Annoying function pointer (2), annoying function pointer

Source: Internet
Author: User

Annoying function pointer (2), annoying function pointer

Link: http://www.cnblogs.com/AnnieKim/archive/2011/12/04/2275589.html

I have previously written an annoying function pointer (I), which summarizes the declaration, definition, and call of common function pointers, as well as the array of function pointers, and the use of function pointers as return values. However, as a C ++ study, I found that the most important thing I missed was the pointer to class members, here we will add some resources (the test environment of the relevant code is vs 2010 ).

Pointers pointing to class members can be divided into two categories and four sub-categories (pointing to data members or member functions, pointing to common members or static members). The following is a one-to-one introduction:

1. pointer to a common member of the class (non-static)

1. pointer to a class member function

To put it simply, the difference between a pointer pointing to a class member function and a normal function pointer is that the former must not only match the parameter type and number of functions, but also

The Return Value Type also matches the class type to which the function pointer belongs. To sum up, compare the following:

A) parameter types and quantity

B) Return Value Type

C) class type (special)

The reason is that non-static member functions must be bound to a class object or pointer to obtain the this pointer of the called object,

Then we can call the member functions referred to by the pointer (we know that all class objects have copies of their own data members, but the member functions are

This pointer is required to distinguish who calls a member function. this pointer is implicitly added to the function parameter list ).

Now that you understand this, it's easy.

Declaration: distinguished from a common function, the pointer to a class member function only needs to add the class type before the pointer. The format is:

Typedef return value (Class Name: * pointer type name) (parameter list );

Value assignment: you only need to assign values using the member function address of the class. format:

Pointer type Name Pointer name = & Class Name: member function name;

Note: The & symbol here is more important: do not add &, the compiler will think that it is here to call the member function, so you need to give the parameter Column

Table. Otherwise, an error is reported. If & is added, the function pointer is obtained. This is a special case for C ++.

Call: The call method is also very simple. For the called object or pointer, use. * And-> * respectively for the call, in the format:

(Class Object. * pointer name) (parameter list );

(Class pointer-> * pointer name) (parameter list );

(Since it is a pointer, whether it is a pointer of the data type or a function pointer, whether it is a common function pointer or a class function member pointer, it should follow

I am used to adding an & get address operator, but the compiler does some special processing for common functions. However, I think it will be a misunderstanding.

In the West, when classes are involved, there will basically be a class domain. When the pointer value should be referenced, we should use * to reference it like a common pointer.

. To remind everyone, because the class's. And-> operators have higher priority than the de-quote operator *, the above can be so clear that readers can

To check the operator priority .)

Note: The first pair of parentheses is very important, because the priority of () is higher than the priority of the member operator pointer.

The following is a simple example:

Class A; typedef void (A: * NONSTATICFUNCPTR) (int); // typedef class A {public: void NonStaticFunc (int arg) {nonStaticMember = arg; cout <nonStaticMember <endl;} private: int nonStaticMember;}; int main () {NONSTATICFUNCPTR funcPtr = & A: NonStaticFunc; A; (. * funcPtr) (10); // call A * aPtr = new A through an object; (aPtr-> * funcPtr) (10); // call return 0 through A pointer ;}

2. pointer to a class data member

The member functions are easy to understand. You only need to determine whether the following two items are consistent:

A) data member type

B) Category

In addition, declarations, assignments, and call methods are similar to the previous one. Let's take another example:

Class A; typedef int (A: * NONSTATICDATAPTR); // typedef class A {public: A (int arg): nonStaticMember (arg) {} int nonStaticMember ;}; int main () {NONSTATICDATAPTR dataPtr = & A: nonStaticMember; A a (10); cout <. * dataPtr; // reference A * aPtr = new A (100) through an object; cout <aPtr-> * dataPtr; // reference return 0 through A pointer ;}

The running results are of course 10 and 100 output respectively.

2. pointer to static members of the class

The difference between static members of a class and common members is that they do not depend on specific objects, and all instantiated objects share the same static member,

Therefore, static members do not have the this pointer concept. Therefore, the pointer to the static member of the class is a normal pointer.

You can see the following example:

Typedef const int * STATICDATAPTR; typedef int (* STATICFUNCPTR) (); // class A {public: static int StaticFunc () {return staticMember ;}; static const int staticMember = 10 ;}; int main () {STATICDATAPTR dataPtr = & A: staticMember; STATICFUNCPTR funcPtr = & A: StaticFunc; cout <* dataPtr; // directly unreference cout <(* funcPtr) (); return 0 ;}

Note that, obviously, to use (& Class Name: member name) to obtain a pointer to a member, the member must first be visible to the outside, that is, public, otherwise, you do not have the permission to obtain ^.


Here, we will summarize the following:

1) there is no difference between static and common function pointers;

2) You can add a class without static limitations.

 

I don't know if there will be any function pointer-related content in the future. Let's finish it first.

Correct the error. I will modify it in time ^.

(End)




Where is the difference between a function pointer and a pointer?

Pointer is a type of data in C language;
A function pointer is a type of C pointer.

Q: "What is the difference between a function pointer and a pointer ?" It indicates that LZ is not familiar with pointers. Modify your question:
"What is the difference between a function pointer and an integer pointer ?"
In this case, I will tell you:
1. Both are pointers
2. The function pointer stores the first address of a function, while the integer pointer stores the address of an integer variable;
3. the pointer itself is a reference type. Therefore, you must release the reference when using it. Function pointers are different from integer pointers for removing references. There are two ways to unreference an integer pointer:
For example, for integer pointer pi:
Int I = 0;
Int * pi = & I;
Method 1: * pi
Method 2: pi [0]
Function pointer pf:
Int f (int );
Int (* pf) (int) = f;
Release reference method: pf (8 );

Method 2 for unreferencing integer pointers: pi [0] is more like an array. It is also more like the method for removing the reference from the function pointer because:
The array name and function name are essentially addresses. Pointers are essentially addresses.
4. In the preceding example, the assignment format is different.
Function pointer pf = f; (no &. In fact, the same is true)
Integer pointer pi = & I;
The reason is that the array name and function name are essentially addresses, while the essence of an integer variable (when used as the left value [if allowed] or the right value) is the value in the address.
5. For function pointers, incremental operations such as pf ++ 99.9999% are generally not allowed.
To access the Function Array, use the function pointer array.

How to use function pointers for parameters?

First, the type of the declared function pointer is declared as follows:
Void (* pFun) (int, int)
Type of the return value of the void function, the name of the pFun pointer, (int x, int y) parameter list of the function.
Second, the function name is the function pointer.
Void hello ()
{
Printf ("hello world! \ N ");
}
Hello is the pointer to this function.
Third, the actual example:
# Include <cstdio>

Void hello (){
Printf ("hello world! \ N ");
}

Void runFun (void (* pFun )()){
PFun ();
}
Void main ()

{
RunFun (hello );
}
First, define the hello function.
Second, define the runFun function. The parameter of this function is a function pointer. As mentioned above, runFun directly uses the passed function pointer to execute the function.
Third, in main, pass the hello function pointer (that is, hello) into runFun.

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.