user63898
user63898

Reputation: 30963

How can i set function result into c++ MACRO

i have problem to set in to c++ MACRO singletone function result . this is what i have :

the macro

#define CCDICT_FOREACH(__dict__, __el__) \
    CCDictElement* pTmp##__dict__##__el__ = NULL; \
    if (__dict__) \
    HASH_ITER(hh, (__dict__)->m_pElements, __el__, pTmp##__dict__##__el__)

and this is how i try to set it :

CCDictElement* pElement = NULL;
CCDICT_FOREACH(GameSingleTone::getInstance()->getGemsDictionary(), pElement)
{
}

the method getGemsDictionary() returns me: CCDictionary*,gemsDictionary;

the compilation error im getting is (on the line of the MACRO):

error C2143: syntax error : missing ';' before '{'

but if i do :

CCDictionary* tempDictionary = CCDictionary::create();
tempDictionary = GameSingleTone::getInstance()->getGemsDictionary();
CCDICT_FOREACH(tempDictionary , pElement)
{
}

every thing is working .
why ?

Upvotes: 0

Views: 86

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103751

Macros simply do text replacement. So when you do this:

CCDICT_FOREACH(GameSingleTone::getInstance()->getGemsDictionary(), pElement)

This line:

CCDictElement* pTmp##__dict__##__el__ = NULL; \

becomes this:

CCDictElement* pTmpGameSingleTone::getInstance()->getGemsDictionary()pElement = NULL;

Which is utter nonsense. This, on the other hand:

CCDICT_FOREACH(tempDictionary , pElement)

translates to this:

CCDictElement* pTmptempDictionarypElement = NULL;

Which is perfectly okay.

Upvotes: 2

Related Questions