Detailed Usage Guide for C ++ member function pointers

Source: Internet
Author: User

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:

 
 
  1. float (SomeClass::*my_memfunc_ptr)(int, char *); 

For member functions modified using the const keyword, the Declaration is as follows:

 
 
  1. 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:

 
 
  1. 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.

 
 
  1. SomeClass *x = new SomeClass;  
  2. (x->*my_memfunc_ptr)(6, "Another Arbitrary Parameter"); 

If the class is on the stack, you can also use the ". *" operator.

 
 
  1. SomeClass y;  
  2. (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.

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.