C ++理解cocos2d-x使用函数指针 [英] C++ understanding cocos2d-x use of function pointers

查看:230
本文介绍了C ++理解cocos2d-x使用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验扩展cocos2d-x CCMenuItem组件,并遇到了一些我以前没有在C ++中看到的东西。如果有人用他们的函数指针声明详细说明什么是有帮助的。



大多数cocos2d-x对象的基类是CCObject,它有以下定义

  class CC_DLL CCObject:public CCCopying 
{
public:
//代码省略
};

//我有一个关于
的问题typedef void(CCObject :: * SEL_SCHEDULE)(float);
typedef void(CCObject :: * SEL_CallFunc)();
typedef void(CCObject :: * SEL_CallFuncN)(CCNode *);
typedef void(CCObject :: * SEL_CallFuncND)(CCNode *,void *);
typedef void(CCObject :: * SEL_CallFuncO)(CCObject *);
typedef void(CCObject :: * SEL_MenuHandler)(CCObject *);
typedef void(CCObject :: * SEL_EventHandler)(CCEvent *);
typedef int(CCObject :: * SEL_Compare)(CCObject *);

#define schedule_selector(_SELECTOR)(SEL_SCHEDULE)(& _SELECTOR)
#define callfunc_selector(_SELECTOR)(SEL_CallFunc)(& _SELECTOR)
#define callfuncN_selector (SEL_CallFuncN)(& _SELECTOR)
#define callfuncND_selector(_SELECTOR)(SEL_CallFuncND)(& _SELECTOR)
#define callfuncO_selector(_SELECTOR)(SEL_CallFuncO)(& _SELECTOR)
#define menu_selector(_SELECTOR)(SEL_MenuHandler)(& _SELECTOR)
#define event_selector(_SELECTOR)(SEL_EventHandler)(& _SELECTOR)
#define compare_selector(_SELECTOR)(SEL_Compare)(& _SELECTOR)

所以在CCObject类之外,但是在cocos2d命名空间中,存在一个函数指针和帮助宏的声明使用它们。我在调用函数指针的这些声明时是否正确?



我理解typedef将一个关键字与一个类型相关联(参见 Typedef函数指针?),返回类型必须为void,函数必须有一个CCObject *的强制参数。但是,我在理解它的适当用法,范围以及C ++如何处理通过另一个函数传递一个函数的方法上迷失了。



问题1 >

我没有按照如何解释声明的函数指针的作用域。在它们的声明中,它们显示由CCObject类限定的函数指针。我应该如何解释这?这是否意味着当它被分配的功能作为成员函数属于CCObject?这是令人困惑的,因为它定义在类体之外,但范围为CCObject。

  typedef void(CCObject :: * SEL_MenuHandler)(CCObject *); 

问题2



在cocos2d-x CCMenuItem类中,它有一个下面定义的静态工厂方法

 这个?是一个函数对待这里的对象吗? 
static CCMenuItem * create(CCObject * rec,SEL_MenuHandler selector);



CCMenuItem * CCMenuItem :: create(CCObject * rec,SEL_MenuHandler selector)
{
CCMenuItem * pRet = new CCMenuItem
pRet-> initWithTarget(rec,selector);
pRet-> autorelease();
return pRet;
}

bool CCMenuItem :: initWithTarget(CCObject * rec,SEL_MenuHandler selector)
{
setAnchorPoint(ccp(0.5f,0.5f));
m_pListener = rec;
m_pfnSelector = selector;
m_bEnabled = true;
m_bSelected = false;
return true;
}

//来自CCMenuItem头的代码段
protected:
CCObject * m_pListener;
SEL_MenuHandler m_pfnSelector; //成员变量存储指向函数的指针?
int m_nScriptTapHandler;
};

所以这个typedef意味着当我传递一个函数时,我传递的值与指针?如果函数没有通过指针,C ++如何处理。是一个函数被视为具有复制构造函数的对象?



我感谢任何帮助和建议。感谢

解决方案

void(CCObject :: *)(CCObject *)方法指针类型(指向成员的指针),而不是普通的函数指针。它是一个指向 CCObject 类的实例方法的指针,它接受 CCObject * 类型的参数。类的类型它的一个方法是指针类型的一部分(由 CCObject :: 表示),类似于参数(因为在下面,指向current对象是所有实例方法的隐藏参数, this )。



typedef 只是定义 SEL_MenuHandler 为该方法指针类型的同义词。



一个方法指针,你需要提供一个实例作为 this 以及参数,使用这样的语法:

  CCObject * object; 
CCObject * anotherObject;
SEL_MenuHandler methodPointer;
(object-> * methodPointer)(anotherObject);
//或等效地:((* object)。* methodPointer)(anotherObject);




如果函数没有通过指针传递,C ++如何处理。
是一个类似于具有复制构造函数的对象的函数?


在C / C ++中, 函数类型或方法类型的表达式。任何时候,当你试图得到的东西函数类型,它会自动转换为指针函数类型。


I am experimenting around with extending cocos2d-x CCMenuItem components and came across something I have not seen before in C++. It would be helpful if someone would elaborate on what is going on with their function pointer declarations

The base class for most cocos2d-x objects is CCObject which has the following definition

class CC_DLL CCObject : public CCCopying
{
public:
    // Code omitted 
};

// The part in which I have a question about
typedef void (CCObject::*SEL_SCHEDULE)(float);
typedef void (CCObject::*SEL_CallFunc)();
typedef void (CCObject::*SEL_CallFuncN)(CCNode*);
typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*);
typedef void (CCObject::*SEL_CallFuncO)(CCObject*);
typedef void (CCObject::*SEL_MenuHandler)(CCObject*);
typedef void (CCObject::*SEL_EventHandler)(CCEvent*);
typedef int (CCObject::*SEL_Compare)(CCObject*);

#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
#define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)
#define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR)
#define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR)
#define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR)
#define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR)
#define event_selector(_SELECTOR) (SEL_EventHandler)(&_SELECTOR)
#define compare_selector(_SELECTOR) (SEL_Compare)(&_SELECTOR)

So outside of the CCObject class, but within the cocos2d namespace, there exists a declaration for function pointers and helper macros to use them. Am I correct in calling these declarations for function pointers?

I understand that typedef is associating a keyword with a type (See Typedef function pointer?) and that the return type is must be void and the function must have one mandatory argument of CCObject *. However I am lost in understanding its appropriate usage, scope, and how C++ treats passing a function through another function.

Question 1

I am not following how to interpret the scope of the declared function pointer. In their declaration, they show the function pointer being scoped by the CCObject class. How should I interpret this? Does this mean when it is assigned that function belongs as a member function to CCObject? This is confusing to me as it is defined outside of the class body but scoped with CCObject.

typedef void (CCObject::*SEL_MenuHandler)(CCObject*);

Question 2

In the cocos2d-x CCMenuItem class, it has a static factory method defined below

// How does C++ treat the this? Is a function treated like an object here?
static CCMenuItem* create(CCObject *rec, SEL_MenuHandler selector);



   CCMenuItem* CCMenuItem::create(CCObject *rec, SEL_MenuHandler selector)
{
    CCMenuItem *pRet = new CCMenuItem();
    pRet->initWithTarget(rec, selector);
    pRet->autorelease();
    return pRet;
}

bool CCMenuItem::initWithTarget(CCObject *rec, SEL_MenuHandler selector)
{
    setAnchorPoint(ccp(0.5f, 0.5f));
    m_pListener = rec;
    m_pfnSelector = selector;
    m_bEnabled = true;
    m_bSelected = false;
    return true;
}

// A snippet from CCMenuItem header
protected:
    CCObject*       m_pListener;
    SEL_MenuHandler    m_pfnSelector; // member variable which stores a pointer to a  function?
    int             m_nScriptTapHandler;
};

So does this typedef mean that when I pass a function it, I am passing by value with a pointer? How would C++ handle this if the function was not passed by pointer. Is a function treated like an object with a copy constructor?

I appreciate any help and advice. Thanks

解决方案

void (CCObject::*)(CCObject*) is a method pointer type (one type of pointer to member), not an ordinary function pointer. It's a pointer that can point to an instance method of the CCObject class that takes a parameter of CCObject* type. The type of class it's a method of is part of the pointer type (denoted by the CCObject::), similar to the parameters (because underneath, a pointer to the "current object" is a hidden parameter to all instance methods, this).

The typedef simply defines SEL_MenuHandler to be a synonym for that method pointer type.

To use a method pointer, you need to provide both an instance to act as this, as well as the arguments, using a syntax like this:

CCObject* object;
CCObject* anotherObject;
SEL_MenuHandler methodPointer;
(object->*methodPointer)(anotherObject);
// or equivalently: ((*object).*methodPointer)(anotherObject);

How would C++ handle this if the function was not passed by pointer. Is a function treated like an object with a copy constructor?

In C/C++, it is not possible to have an expression of "function type" or "method type". Any time you try to get something of "function type", it is automatically converted to a "pointer to function" type.

这篇关于C ++理解cocos2d-x使用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆