We have introduced in detail questions about C ++ function pointers in an article, so today we will have a better grasp of this knowledge. In C ++ programs, many functions are member functions, that is, these functions are part of a class. You cannot point to a member function as a normal function pointer. The correct way is to use a member function pointer. The pointer of a member function points to a member function in the class and has the same parameters as before. The declaration is as follows:
- float (SomeClass::*my_memfunc_ptr)(int, char *);
For member functions modified using the const keyword, the Declaration is as follows:
- float (SomeClass::*my_const_memfunc_ptr)(int, char *) const;
- C ++ const variable usage tips
- Detailed application guide for C ++ static keywords
- Overview of C ++ Cstring Application Methods
- Analysis of C ++ typeof Basic Application Methods
- Introduction to the C ++ access control operator
Note that special operators are used: *), while "SomeClass" is part of the Declaration. C ++ member function pointers have a terrible restriction: they can only point to member functions in a specific class. For the combination of each parameter, different member function pointer types are required. For each function that uses const modifier and function in different classes, different function pointer types are also required. In MSVC, there is a different call type for the following four call methods:
_ Cdecl, _ stdcall, _ fastcall, and _ thiscall.
_ Thiscall is the default method. It is interesting that there is no detailed description of the _ thiscall keyword in any official documents, but it often appears in the error message. If you explicitly use it, you will see the error message "it is reserved for future use .)
If you use a C ++ member function pointer, you 'd better use typedef to prevent confusion. To point a function pointer to a function of Type float SomeClass: some_member_func (int, char *), you can write:
- my_memfunc_ptr = &SomeClass::some_member_func;
Many compilers, such as MSVC, will remove "&", while other compilers, such as gnu g ++, need to add "&". so I suggest adding it to the handwriting program. To call a member function pointer, you must first create an instance of SomeClass and use the special operator "-> *". This operator has a low priority, you need to place it in parentheses as appropriate.
- SomeClass *x = new SomeClass;
- (x->*my_memfunc_ptr)(6, "Another Arbitrary Parameter");
If the class is on the stack, you can also use the ". *" operator.
- SomeClass y;
- (y.*my_memfunc_ptr)(15, "Different parameters this time");
Don't blame me for using such a strange syntax-it seems that C ++ designers have sincere feelings for punctuation! C ++ adds three Special operators compared with C to support member pointers. ": *" Is used to declare the pointer, and "-> *" and ". *" are used to call the function pointed to by the pointer. It seems that excessive attention to the vague and rarely used part of a language is unnecessary. Of course, you can reload the "-> *" operators, but this is not the scope involved in this article .)
A c ++ member function pointer can be set to 0, and can use "=" and "! = "Comparison operator, but this comparison can only be performed between pointers of member functions in the same class. Any member function pointer can be compared with 0 to determine whether it is null. Unlike function pointers, unequal operators <, >,<=, >=) are not available for C ++ member function pointers.