Application of member function pointer the callback mechanism inside the UIButton (iii)

Source: Internet
Author: User


main.cpp//basic////Created by, on 15/1/9.//Copyright (c) 2015 Huangyongrui. All rights reserved.//#include <iostream>using namespace Std;class uibutton;//struct nsobject{//class bool INI for memory management    T () {return true;    }};//Hero class struct hero:public nsobject{void Move (uibutton* button) {cout << "hero moved" << Endl;  The alias of the}};//member function pointer, typedef void (nsobject::* FUNC) (uibutton*), #pragma the variable write permission in the struct is public by default. Class, do not write permissions, the default is private. C + + inheritance is a single colon: public is publicly inheriting struct UIButton:p ublic nsobject{Private://member variable is generally private, and then gives it a public set Get interface method int _tag    ; NSObject *_target;//This just pointer should also be created for him, where to create? Initialize method inside?? No, because if you want 2 heroes, wouldn't it be to create 2 buttons??        The FUNC _func should be passed in when the button is created; Public://For OC There is a class method for creating objects + #pragma dynamically passing in objects and methods as long as the address of the callback method satisfying this format can be passed static uibutton* Buttonwithtarget (NSObject *ta Rget,func FUNC) {//Create object//The first step is to open up space UIButton *m_button = new uibutton;//here is not like OC Alloc created counter on +1, there is no concept of reference counter in C + + so there is no minus one ...     Open space is new, release on delete           The second step is to initialize the IF (M_button->initwithtarget (Target,func)) {//pointer access method with arrows///If it is in OC it will also call Autor        Elease ()//third returns the pointer of the object return M_button;        }//If initialization failed//destruction of space delete M_button;                Prevent wild pointer M_button = nullptr;            return NULL; }//Return Boolean to determine if initialization succeeded bool Initwithtarget (NSObject *target,func FUNC) {#pragma note the principle subclass initialization must first call the parent class's initialization method ( Because the initialization of the parent class may assign a value to a variable, if you do not invoke the initialization of the parent class, then you will inherit no value) if (!        Nsobject::init ()) {return false;//parent class initialization failed}//Initialize subclass own things _target = target;            _func = func;//return true;    }//member variable set get method int Gettag () {return _tag; } void Settag (int tag) {//If there is such a method in oc Self.tag = tag; Is there a problem? Yes!        Dead Loop!    _tag = tag;    }//Analog click button when void Touch () {cout << "button is in point" << Endl; }//I now want the hero to move in the point (not to print a word, andIs the heroic object calls his own Move method) void Touch1 () {#pragma This function pointer requires a parameter parameter type is UIButton takes itself out into the callback function (_target->* _func) (                this);    void func (uibutton*); }};//struct plane:public nsobject{void Fly (uibutton* button) {cout << "aircraft??    Flying "<< Endl;    }};//main function int main () {#pragma problem//But now I want to put the button on the plane and click Let the plane move so I'm not going to change the source file hero to a plane?? That means I want to button general, what to do?        The object that the button represents can only be retrieved dynamically, and the execution method also needs to dynamically acquire _hero-a move ();    Hero objects should be presented as arguments, but the parameter types cannot be fixed as heroes, but NSObject types, whether aircraft tank cannons can be passed in//create heroes hero* Hero = new Hero;        Create button UIButton *button = Uibutton::buttonwithtarget (Hero, (FUNC) &hero::move);//cast button->touch1 ();    Create aircraft object Plane *plane = new Plane;    UIButton *button2 = uibutton::buttonwithtarget (plane, (FUNC) &plane::fly);        Button2, Touch1 (); The method selector is actually a macro #define selector (SET) (FUNC) &AMP;SET//set macro parameter//create button UIButton *button3 = Uibutton::buttonwit Htarget (Hero, selector (Hero::Move));        Button3->touch1 (); return 0;}


Application of member function pointer the callback mechanism inside the UIButton (iii)

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.