Refer to the relevant chapter of "Pointer to a class member function", which includes the term "class member function", but strictly speaking, the member function here only refers to a non-static member function, this term also contains the term
In C + +, a pointer to a member function is a very special thing. For a normal function pointer, it can be treated as an address and can be arbitrarily converted and called directly when needed. However, for member functions, general type
Http://blog.csdn.net/hairetz/article/details/4153252
First look at this piece of code
Class Test{Public:Test (int I) {m_ I = I ;}Test (){};Void Hello (){Printf ("Hello/N ");}PRIVATE:Int m_ I;};
Int main (){Test * P = new test ();P-> Hello ();P =
Recently, function pointers have been used in development, so I want to sort out the concept of function pointers. O (distinct _ distinct) O ~
First, the function pointer is a pointer to a group of functions of the same type. We can also think
normal function pointer: "Return_type (*ptr_name) (para_types)"Class member function pointer: "Return_type (class_name::* ptr_name) (para_types)"Class data member pointer: "type class_name::* ptr_name";C + +:1 classDemo2 {3 Public:4Demo ():d ATA ( -
Class member function pointers:Used to access class member functions, which differ from general function pointers.A class member function handles a class data member, declaring a class member function pointer at the same time, and also pointing out
///to implement an array of class member function pointers, see the code below, and also note that static array members can not declare array size within a class class Cmymath { Public: int ADD (int A, int b); int Sub (int a, int b);
Abstract: This section describes how to obtain the member function address and call the address.
Keyword: C ++ member function this pointer call Convention
I. Usage of member function pointers
In C ++, the pointer to a member function is special.
To use a class member function as a C callback function
Ask a question:Callback functions are based on the C programming Windows SDK Technology, not for C + +, programmers can be a C function directly as a callback function, but if you try to
One, the use of member function pointers
In C + +, a pointer to a member function is a very special thing. For ordinary function pointers, it can be treated as an address and can be arbitrarily converted and invoked when needed. However, for member
A function can be called directly in the C language using the function address:void print () { printf ("function print");} typdef void (*fun) (); Fun F = print;f ();class non-static member functions in C + + must be called through an instance,
Let's take a look at the simplest function:
void foo(int a){ cout
Its function pointer type is
void (*)(int);
We can use this method as follows:
void (*pFunc)(int) = &foo;pFunc(123);
This is the basic usage of function pointers.
In C language, you can use the function address to directly call a function:Void print () {printf ("function print");} typdef void (* fun) (); fun f = print; f ();In C ++, non-static class member functions must be called through instances. In C ++,
We know that in C + +, if a variable is declared as a const type, the operation that attempts to modify the value of the variable is considered a compilation error. For exampleConst Char blank='; blank='\ n'; // Error In object-oriented
Recently learned the principle of plug, mainly some memory operation. And then I found one in the process--the address of a class member function seems to get out of the class directly (except for static functions).I've found an ingenious way to get
A callback that implements a class member function is not a static function;#ifndef __callback_proxy_h_#define__callback_proxy_h_Templateclassccallbackproxy{typedefvoid(TObject::* cbfun) (tparam*); Public:
The callback function is called by the caller.
A callback function is a function called by a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to
When looking at the open source code, often see in the class member function after add const, before have not too concerned about, recently idle to have no matter, think of this matter, on-line check a bit, probably understand is how, here quoted
Each class member function involves only one object, that is, the object that invokes it. However, sometimes a method might involve two objects, in which case you need to use the this pointer of C + +Assuming that the method is named Topval (), the
When a class member function creates a thread to invoke another function of a class member, there are two ways to fix it.
The first kind is more troublesome,For example, there is a classvoid* Gstartvod (void* pvoid);Class record{Friend void*
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.