Cocos2d-x中如何在CREATE_FUNC中指定参数
How to specify argument in CREATE_FUNC in Coco2d-x
我的MessageBoxes
class在构造函数中传递了一个参数,我希望知道如何指定参数,在这个上下文中是std::string _content
,在CREATE_FUNC()
?
我得到的错误是 "constructors cannot be declared 'static'"
这是MessageBoxes.h的代码:
class MessageBoxes : public cocos2d::Node
{
private:
Sprite* _sprite;
bool _speaking;
float _speakingTime;
std::string _content;
public:
CREATE_FUNC(MessageBoxes(std::string content));
protected:
virtual bool init(std::string _content);
void setSprite();
void setContent();
};
CREATE_FUNC是在CCPlatformMacros.h
中定义的预定义宏
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
代码
CREATE_FUNC(MessageBoxes(std::string content));
实际上是
new(std::nothrow) MessageBoxes(std::string content)();
在c++中有编译错误。
但是你可以自己写类似CREATE_FUNC的创建函数,比如
static MessageBoxes* create(std::string content) {
MessageBoxes* ret = new(std::nothrow) MessageBoxes();
if(ret && ret->init(content)) { //<----Or anything you wanna init with
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr;
return nullptr;
}
}
我的MessageBoxes
class在构造函数中传递了一个参数,我希望知道如何指定参数,在这个上下文中是std::string _content
,在CREATE_FUNC()
?
我得到的错误是 "constructors cannot be declared 'static'"
这是MessageBoxes.h的代码:
class MessageBoxes : public cocos2d::Node
{
private:
Sprite* _sprite;
bool _speaking;
float _speakingTime;
std::string _content;
public:
CREATE_FUNC(MessageBoxes(std::string content));
protected:
virtual bool init(std::string _content);
void setSprite();
void setContent();
};
CREATE_FUNC是在CCPlatformMacros.h
中定义的预定义宏#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
代码
CREATE_FUNC(MessageBoxes(std::string content));
实际上是
new(std::nothrow) MessageBoxes(std::string content)();
在c++中有编译错误。
但是你可以自己写类似CREATE_FUNC的创建函数,比如
static MessageBoxes* create(std::string content) {
MessageBoxes* ret = new(std::nothrow) MessageBoxes();
if(ret && ret->init(content)) { //<----Or anything you wanna init with
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr;
return nullptr;
}
}