Does anyone know what is the difference between CC_CALLBACK_0 and CC_CALLBACK_1 or CC_CALLBACK_2?


#1

As mentioned in subject


#2

The number means how many parameters the callback function has.
CC_CALLBACK_0 means callback function with no input parameter, such as MyClass::doSomething()
CC_CALLBACK_1 means there’s only 1 input parameter, e.g., MyClass::doSomething(int a)


#3

Ah, thanks :slight_smile:


#4

Zhe Wang wrote:

Why won’t use use variadic templates instead of those verbose macros; just to make everyone’s life easier? I mean something like this: https://gist.github.com/kuhar/8163865


#5

Hey, please go over this with me.

For this method:

void checkGameOver(const TurnInfo turnInfo);

If I try to do CC_CALLBACK_1:

CallFunc::create(CC_CALLBACK_1(GameScene::checkGameOver, this, turnInfo));

I get a compilation error:
No matching function for call to ‘create’.

But if I do CC_CALLBACK_0:

CallFunc::create(CC_CALLBACK_0(GameScene::checkGameOver, this, turnInfo))

Even though checkGameOver takes 1 parameter, the error goes away and it works.

Clearly the explanation:

Doesn’t quite make sense, am I not understanding something very basic here? Every version of cocos (starting at 3.0) that I’ve used has behaved this way.


#6

Just use CC_CALLBACK_0. You can pass it as many parameters as you want.
CC_CALLBACK_1 and CC_CALLBACK_2 have placeholders, so after you define your CC_CALLBACK_X function, you can pass variables to that function.


#7

CC_CALLBACK_1 just wraps std::bind and adds a std::placeholder::_1 to the bind. You aren’t putting in a placeholder but an actual value.

As the previous poster has pointed out, you can use CC_CALLBACK_0 as it has a VA_ARGS parameter in the macro that just places the arguments after “this” as bound parameters:

    // new callbacks based on C++11
#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__)

#8

Thanks, I use CC_CALLBACK_0 all the time, and have looked at the macro definitions. Just thought the explanation originally given was confusing.