How do I declare and use a pointer to a class member function?
2010-04-07 17:04
531 查看
How do I declare and use a pointer to a class member function? (top) The syntax is similar to a regular function pointer, but you also have to specify the class name. Use the .* and ->* operators to call the function pointed to by the pointer.
Collapse class CMyClass
{
public:
int AddOne ( unsigned n ) { return n+1; }
int AddTwo ( unsigned n ) { return n+2; }
};
main()
{
CMyClass myclass, *pMyclass = &myclass;
int (CMyClass::* pMethod1)(unsigned); // Full declaration syntax
pMethod1 = CMyClass::AddOne; // sets pMethod1 to the address of AddOne
cout << (myclass.*pMethod1)( 100 ); // calls myclass.AddOne(100);
cout << (pMyclass->*pMethod1)( 200 ); // calls pMyclass->AddOne(200);
pMethod1 = CMyClass::AddTwo; // sets pMethod1 to the address of AddTwo
cout << (myclass.*pMethod1)( 300 ); // calls myclass.AddTwo(300);
cout << (pMyclass->*pMethod1)( 400 ); // calls pMyclass->AddTwo(400); //Typedef a name for the function pointer type.
typedef int (CMyClass::* CMyClass_fn_ptr)(unsigned);
CMyClass_fn_ptr pMethod2;
// Use pMethod2 just like pMethod1 above....
} The line Collapse
int (CMyClass::* pMethod1)(unsigned);
pMethod1 is a pointer to a function in CMyClass; that function takes an unsigned parameter and returns an int. Note that CMyClass::AddOne is very different from CMyClass::AddOne(). The first is the address of the AddOne method in CMyClass, while the second actually calls the method.
Collapse class CMyClass
{
public:
int AddOne ( unsigned n ) { return n+1; }
int AddTwo ( unsigned n ) { return n+2; }
};
main()
{
CMyClass myclass, *pMyclass = &myclass;
int (CMyClass::* pMethod1)(unsigned); // Full declaration syntax
pMethod1 = CMyClass::AddOne; // sets pMethod1 to the address of AddOne
cout << (myclass.*pMethod1)( 100 ); // calls myclass.AddOne(100);
cout << (pMyclass->*pMethod1)( 200 ); // calls pMyclass->AddOne(200);
pMethod1 = CMyClass::AddTwo; // sets pMethod1 to the address of AddTwo
cout << (myclass.*pMethod1)( 300 ); // calls myclass.AddTwo(300);
cout << (pMyclass->*pMethod1)( 400 ); // calls pMyclass->AddTwo(400); //Typedef a name for the function pointer type.
typedef int (CMyClass::* CMyClass_fn_ptr)(unsigned);
CMyClass_fn_ptr pMethod2;
// Use pMethod2 just like pMethod1 above....
} The line Collapse
int (CMyClass::* pMethod1)(unsigned);
pMethod1 is a pointer to a function in CMyClass; that function takes an unsigned parameter and returns an int. Note that CMyClass::AddOne is very different from CMyClass::AddOne(). The first is the address of the AddOne method in CMyClass, while the second actually calls the method.
相关文章推荐
- How to use the pointer of function in a class?
- How to declare and initialize a static const array as a class member?
- How do you define a copy constructor or assignment operator for a class that contains a pointer to a (abstract) base class?
- How To Use XDOLoader to Manage, Download and Upload Files? (DOC ID 469585.1)
- How To Use Replace function in TEXT and NTEXT fields
- Question 20: Which allocator member function do standard containers use to acquire storage for their elements in C++?
- pointer to function and pointer to member function
- How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)
- (F#) How to declare variable and function.
- How do I use locales and resource bundles to internationalize my application?
- experiment : use class member function pointer
- How To Use Function 'F4_FILENAME' and 'KD_GET_FILENAME_ON_F4'
- Why do we need smart pointer and how to implement it.
- ACC97: How to Use Percent, Plus, and Caret Symbols in ASP Forms
- Introduction to COM - What It Is and How to Use It.(ZT)
- How to use .netrc file on windows to save user and password
- how to call the member function?( C++ ) . or ->
- how to save a c++ object in java object and use it
- Function Pointer of class member function (类的成员函数指针)
- How to use QToolBar and QToolButton in Qt