cocos2dx中函数指针传递的方法
目的 看到群里有个朋友搞了好几天函数指针传递,没搞好。所以写一篇文章,旨在从cocos2dx中帮朋友们找到如何传递指针。 旧版本的函数指针传递 全局函数函数指针调用 一般在C11之前,我们一般是这样定义一个函数指针类型。 [cpp] view plaincopyprint? typede
目的
看到群里有个朋友搞了好几天函数指针传递,没搞好。所以写一篇文章,旨在从cocos2dx中帮朋友们找到如何传递指针。
旧版本的函数指针传递
全局函数函数指针调用
一般在C++11之前,我们一般是这样定义一个函数指针类型。
[cpp] view
plaincopyprint?
- typede void(*pFunc)(int,...);
什么意思呢?
[cpp] view
plaincopyprint?
- typedef void/*return type of function*/
[cpp] view
plaincopyprint?
- (*pFunc/*the pointer of function*/)
[cpp] view
plaincopyprint?
- (int,.../*the types of function parameters*/);
- typedef void/*函数返回类型*/(*pFunc/*函数指针*/)(int,.../*函数参数类型*/);
OK,那么好了,该如何调用呢?
一般来说是像下面这样的。
[cpp] view
plaincopyprint?
- typedef void(*pFunc)();
- void fA(){ };
- void fB(pFunc pf){ (*pf)(/*里面加函数参数*/) };
- void fC(){ fB(&fA);};
即为在fC中调用fB,fB的参数为fA指针。
成员函数函数指针的调用
那么成员函数如何调用呢?
只需要加一个类名修饰符即可。
示例如下:
[cpp] view
plaincopyprint?
- class C;
- typedef void(C::*pFunc)();
- void C::fA(){};
- void C::fB(pFunc pf){ (this->*pf)()};
- void C::fC(){this->fB(&C::fA);};
其实,有心的朋友应该会注意到cocos2dx 版本中的各种selector即为宏定义的函数指针的引用,定义如下:
[cpp] view
plaincopyprint?
- 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<:sel_callfunc>(&_SELECTOR)
- #define callfuncN_selector(_SELECTOR) static_cast<:sel_callfuncn>(&_SELECTOR)
- #define callfuncND_selector(_SELECTOR) static_cast<:sel_callfuncnd>(&_SELECTOR)
- #define callfuncO_selector(_SELECTOR) static_cast<:sel_callfunco>(&_SELECTOR)
- #define menu_selector(_SELECTOR) static_cast<:sel_menuhandler>(&_SELECTOR)
- #define schedule_selector(_SELECTOR) static_cast<:sel_schedule>(&_SELECTOR)
所以不懂函数指针的朋友完全可以模仿它。 相信你很快就能上手。
C++11 中std::function的应用
cocos2dx 里面std::function定义的各种回调的解析
假设我们不知道std::function如何使用,那么只有浏览cocos2dx3.X里面的源码,我们会发现有大量的callBack 是用std::function定义的。
我们在此,首先用cocos2dx里面的网络http请求的返回函数举例。
HttpRequest 的回调定义为
inline void setResponseCallback(const ccHttpRequestCallback& callback)
{
_pCallback = callback;
}
追踪ccHttpRequestCallback,可以发现ccHttpRequestCallback即为std::function定义的:
typedef std::function
使用过的同学应该知道怎么调用的,
一般都是 setResponseCallback(CC_CALLBACK_2(ClassName::jsonRequestCompleted,this));
CC_CALLBACK是什么东东,其实就是std::bind的引用宏定义。我们查看定义如下:
[cpp] view
plaincopyprint?
- #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__)
很明显,CC_CALLBACK_2就是 std::bind里面传参数,第一个是引用参数表示函数,第二个是目标,第三个,第四个是占位符,后面是不定参数。
所以可以等价代换为std::bind,那么我们上面的回调可以变成
setResponseCallback(std::bind(&ClassName::jsonRequestCompleted,this,std::placeholders::_1,std::placeholders::_2));
自定义std::function的应用
通过以上分析,相信大家已经掌握了如何通过std::function传递函数,以及std::bind去调用。不过为了照顾一些基础薄弱的朋友,我还是给出一个简单的例子。
[cpp] view
plaincopyprint?
- class C;
- void C::fA(){}
- void C::fB(const std::functionvoid()> &func)
- {
- if (func)
- {
- func();
- }
- }
- void C::fC()
- {
- fB(std::bind(&c::fA,this));
- }
关于非成员函数使用std::function
非成员函数使用std::function和上面的函数指针实际上是一致的,鉴于它比较容易,就不在此赘述了,还不会的朋友可以试一下。
申明:
http://blog.csdn.net/q229827701/article/details/41479753

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
