Find the function pointer passing method from cocos2dx

Source: Internet
Author: User
The purpose is to see a friend in the group who has passed the function pointer for several days. So I wrote an article to help my friends find out how to pass pointers from cocos2dx. The old version of the function pointer transfers the global function pointer call before C11. We generally define a function pointer type like this. Typeworker ID (* pFunc) (int ,...);

The purpose is to see a friend in the group who has passed the function pointer for several days. So I wrote an article to help my friends find out how to pass pointers from cocos2dx. The old version of the function pointer transfers the global function pointer call before C11. We generally define a function pointer type like this. Typede void (* pFunc) (int ,...);

Purpose

I saw a friend in the group who had passed the function pointer for several days. So I wrote an article to help my friends find out how to pass pointers from cocos2dx.

The old version of the function pointer transfers the global function pointer call.

Generally, before C ++ 11, we define a function pointer type in this way.


typede void(*pFunc)(int,...);

What does it mean?

typedef  void/*return type of function*/
(*pFunc/*the pointer of function*/)
(Int ,... /* the types of function parameters */); typedef void/* function return type */(* pFunc/* function pointer */) (int ,... /* function parameter type */);


OK. So, how can I call it?

In general, it is like the following.

Typedef void (* pFunc) (); void fA () {}; void fB (pFunc pf) {(* pf) (/* adding function parameters */)}; void fC () {fB (& fA );};

That is, to call fB in fC, the fB parameter is the fA pointer.

Call of member function pointer

How can we call a member function?

You only need to add a class name modifier.

Example:

class C;typedef void(C::*pFunc)();void C::fA(){};void C::fB(pFunc pf){ (this->*pf)()};void C::fC(){this->fB(&C::fA);};


In fact, some interested friends should notice that various selectors in cocos2dx are referenced by Macro function pointers, which are defined as follows:

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
 
  (&_SELECTOR)#define callfuncN_selector(_SELECTOR) static_cast
  
   (&_SELECTOR)#define callfuncND_selector(_SELECTOR) static_cast
   
    (&_SELECTOR)#define callfuncO_selector(_SELECTOR) static_cast
    
     (&_SELECTOR)#define menu_selector(_SELECTOR) static_cast
     
      (&_SELECTOR)#define schedule_selector(_SELECTOR) static_cast
      
       (&_SELECTOR)
      
     
    
   
  
 


So friends who do not know the function pointer can imitate it completely. I believe you will be able to get started soon.

Parsing of various callbacks defined by std: function in cocos2dx in C ++ 11

If we don't know how to use std: function, we only need to browse the source code in cocos2dx3. X, and we will find that a large number of callBack are defined by std: function.

Here, we first use the network http request return function in cocos2dx as an example.

The callback of HttpRequest is defined

Inline void setResponseCallback (const ccHttpRequestCallback & callback)
{
_ PCallback = callback;
}

Tracking ccHttpRequestCallback, you can find that ccHttpRequestCallback is defined by std: function:

Typedef std: function CcHttpRequestCallback;

Students who have used it should know how to call it,

SetResponseCallback (CC_CALLBACK_2 (ClassName: jsonRequestCompleted, this ));

What is CC_CALLBACK? It is actually the macro definition referenced by std: bind. The definition is as follows:

#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)


Obviously, CC_CALLBACK_2 is the parameter passed in std: bind. The first parameter is the reference parameter to indicate the function, the second parameter is the target, the third parameter is the placeholder, and the second parameter is the variable parameter.

So it can be equivalent to std: bind, so the above callback can be changed

SetResponseCallback (std: bind (& ClassName: jsonRequestCompleted, this, std: placeholders: _ 1, std: placeholders: _ 2 ));


Custom std: function Application

Through the above analysis, we believe that you have mastered how to pass functions through std: function and std: bind to call. However, I would like to give a simple example to take care of some friends with weak foundations.

class C;void C::fA(){}void C::fB(const std::function
 
   &func){if (func){func();}}void C::fC(){  fB(std::bind(&c::fA,this));}
 

About non-member functions using std: function
Non-member functions use std: function and the above function pointer are actually the same. In view of its ease, we will not repeat it here, but other friends can give it a try.
Statement: 
http://blog.csdn.net/q229827701/article/details/41479753

Zookeeper

Zookeeper

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.