cocos2d-x3.1 callback function specific explanation

Source: Internet
Author: User

cocos2d-x3.1 the definition of the callback function CCRef.h declaration. Source code, for example, the following:

typedef void (ref::* Sel_callfunc) (), typedef void (ref::* SEL_CALLFUNCN) (node*), typedef void (ref::* SEL_CALLFUNCND) ( node*, void*), typedef void (ref::* Sel_callfunco) (ref*), typedef void (ref::* Sel_menuhandler) (ref*); typedef void (ref:: *sel_schedule) (float), #define Callfunc_selector (_selector) static_cast<cocos2d::sel_callfunc> (&_ SELECTOR) #define Callfuncn_selector (_selector) static_cast<cocos2d::sel_callfuncn> (&_selector) #define Callfuncnd_selector (_selector) static_cast<cocos2d::sel_callfuncnd> (&_selector) #define CallfuncO_ Selector (_selector) static_cast<cocos2d::sel_callfunco> (&_selector) #define Menu_selector (_SELECTOR) Static_cast<cocos2d::sel_menuhandler> (&_selector) #define Schedule_selector (_selector) static_cast< Cocos2d::sel_schedule> (&_selector)

As a matter of fact. This is the application of the function pointer.

The following uses a simple sample to illustrate the use of function pointers:

typedef void (*P) (int i); void func (int i) {    printf ("%d\n", I);} p = func;  P (1);
A new type is defined by typedef p,p is a function pointer with an int value for the function's parameters. The return void,p pointer can point to this function. The function name is a pointer, so p is directly equal to Func, then P (1), run

Through this example can be found. This is also true of Cocos2d-x's callback function. 6 function pointer types are defined first. Can only point to a member function of a ref class. Then define a definition of the macro that calls these function pointers. These macro definitions are called back by the callback object's Cc_callback_, and here is a sample.

#include <iostream>  //callback.husing namespace std;//defines a base class Person{public:    void Print (string name);//define the base class method};//typedef a function pointer type typedef void (person::* sel_callfun) (string str);//define a derived class class Student:public Person{private:    string m_name;    int m_age;public:    Student (string name, int age);    ~student ();        void CallBack (String str);//define a callback function    void result ();//print result protected:    person* m_plisten;//callback function Run Object    sel_ Callfun m_pfnselectior;//callback function pointer};

#include "CallBack.h"  //callback.cpp//defines a base class void Person::p rint (string name) {    cout << name << Endl ;} Student::student (string name, int age) {    this->m_name = name;    This->m_age = age;} Student::~student () {    }void Student::result () {    cout << "Hi This is a Student" << Endl;        M_pfnselectior = (sel_callfun) (&student::callback);//First turn the callback function of the subclass into a function pointer        m_plisten = this;//The Run object is this        m_ Plisten->print (m_name);//First run the print function (M_plisten->*m_pfnselectior) of the parent class (        m_name);//Run the callback function, that is, the callback function of the subclass }void student::callback (String str) {    cout << ' My name is ' << str << ' age is ' << m_age << ; Endl;}
#include <iostream>//main.cpp#include "CallBack.h" int main (int argc, const char * argv[]) {    //Insert code here ...    student* s = new Student ("yxk", +);    S->result ();    return 0;}

The subclass of the callback function by demonstrating a sample person object




cocos2d-x3.1 callback function specific explanation

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.