C + + pointer to a function of a class member function

Source: Internet
Author: User

//ConsoleApplication34.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;classParent { Public: Parent () {//cout << "I am Dad" << Endl;    }    Virtual voidprint () {cout<<"I'm the Father ."<<Endl; }Private:    intA;};classChild: PublicParent { Public: Child () {}voidprint () {cout<<"I'm a son ."<<Endl; }Private:    intb;};intMain () {//return value type (Domain action::* function pointer name) (method parameter signature) This one is not a parameter.//void (Child::* fptr) (), non-parametric//void (Child::* fptr) (int,int);//Explanation: No return value type scope is a function pointer that belongs to the child class, that is, a function pointer to any parameterless member function that points to the child class    void(Child::*fptr)    (); //a function pointer that eventually binds to the print function of the child classFptr= &Child ::p rint;; Child arr[]={child (), child (), Child ()}; //Parent *p; //child* C; //p = arr; //C = arr; //P->print (); //C->print (); //p++; //C + +; //P->print (); //C->print (); //p++; //C + +; //P->print (); //c->print ();        (*ARR.*FPTR) ();//equivalent to Arr[0].print ();    (* (arr+1).*fptr)    (); (* (arr +2).*fptr)    (); System ("Pause"); return 0;}

C + + pointer to a function of a class member function

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.