Member function pointer of C ++ class

Source: Internet
Author: User

//************************************** *******************
// Member function pointer variables of Clause 1 can be strongly converted to each other.
//************************************** *******************

Class A {}; Class B {}; Class C {}; typedef void (A: * afun) (void); typedef int (B: * bfun) (INT, INT); typedef void (C: * cfun) (double); afun pafun; bfun pbfun; cfun pcfun; // No relationship exists between Class A, Class B, and class C, although the following conversions are OK, there is basically no significance for pafun = (afun) pbfun; // okpafun = (afun) pcfun; // okpbfun = (bfun) pafun; // okpbfun = (bfun) pcfun; // okpcfun = (cfun) pafun; // okpcfun = (cfun) pbfun; // OK

 

 

//************************************** *******************
// Use of member function pointers in Clause 2
//************************************** *******************

// Definitions of Class A and Class B Class A {public: void test () {cout <"A: Test ()" <Endl ;}}; Class B: public A {public: void test () {cout <"B: Test ()" <Endl ;}}; // define the class member function pointer typedef void (A: * a_mfun) (void); typedef void (B: * B _mfun) (void); // codea; b; a_mfun pafun = & (A: Test); // Note: The test member function must be public; otherwise, the B _mfun pbfun = & (B: Test) error is returned ); // Note: The test member function must be public. Otherwise, an error is returned (. * pafun) (); // Output A: Test () (B. * pbfun) (); // output B: Test ()
(B. * pafun) (); // Output A: Test ()
A * pA = & A; B * pb = & B; (Pa-> * pafun) (); // Output A: Test () (Pb-> * pbfun) (); // output B: Test () (Pb-> * pafun) (); // Output A: Test (), B * variables can be assigned to a * variables.

// ******* Error ******
(Pa-> * pbfun) (); // error: cannot convert from 'A * 'to' B *'
// Error: cannot dereference a 'B _ mfun' on a 'a *'
//*****************

// ******* OK ******
B * pb_new = (B *) Pa; // strong conversion, but essentially missing
(Pb_new-> * pbfun) (); // output B: Test (). Although OK, remember not to use it like this. This is only used as an experiment demonstration.
// Essentially, a member function of a class is a function with the this pointer, but some restrictions will be added to the compiler during use,
// For example, a member function pointer of a class cannot be forced to convert to a common variable pointer or a common function pointer variable. This is mentioned in the previous article.
//**************
// The following is truly meaningful. It is used in the Framework mechanism of MFC. // The base class variable pointer + base class member function pointer is used to call the sub-class function a_mfun pafun_ B = (a_mfun) & (B: Test); A * pa_ B = & B; (pa_ B-> * pafun_ B) (); // output B: Test () // example: // The message ing in the dialog box includes on_bn_clicked (idc_btn_new_dlg, & ctestdlg: onbnclickedbtnnewdlg) // map the RESPONSE event of the button with ID as response to the onbnclickedbtnnewdlg member function of ctestdlg // we will study on_bn_clicked # define on_bn_clicked (ID, memberfxn) \ on_control (bn_clicked, ID, memberfxn) # define on_control (wnotifycode, ID, memberfxn) \ {wm_command, (Word) wnotifycode, (Word) ID, (Word) ID, afxsigcmd_v, \ (static_cast <afx_pmsg> (memberfxn)}, typedef void (afx_msg_call c1_target: * afx_pmsg) (void ); // you can see that the address of the subclass member function is assigned to the variable of the pointer type of the base class member function.
Related Article

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.