简体   繁体   中英

How should I pass NULL to the va_list function parameter?

I wanna pass NULL to the 4th param of the following function:

bool CCMenuItemToggle::initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, **va_list args**);

like this:

CCMenuItemToggle::initWithTarget(this, menu_selector(GOSound::toggleButtonCallback), NULL, NULL);

It's ok when I build it in XCode (clang3.1). But when I port the code to android ndk (g++4.7), it fails to compile:

no viable conversion from 'int' to 'va_list' (aka '__builtin_va_list')

How should I deal with it?

I assume your code will work if you just use an empty va_list instead of NULL.

CCMenuItemToggle::initWithTarget( this, menu_selector(GOSound::toggleButtonCallback)
                                , NULL, va_list() );

Edit: Maybe this alternative solution works with both compilers.

va_list empty_va_list = va_list();
CCMenuItemToggle::initWithTarget( this, menu_selector(GOSound::toggleButtonCallback)
                                , NULL, empty_va_list );

I see this question was answered but it is not standard. the following code will through a runtime error in Visual Studios; however, it works fine with g++.

    va_list empty_va_list;
CCMenuItemToggle::initWithTarget( this, menu_selector(GOSound::toggleButtonCallback), NULL, empty_va_list );

A better solution would be to create a couple helper functions that construct an empty va_list.

va_list CCMenuItemToggle::createEmptyVa_list()
{
   return doCreateEmptyVa_list(0);
}

va_list CCMenuItemToggle::doCreateEmptyVa_list(int i,...)
{
    va_list vl;
    va_start(vl,i);
    return vl;
}

make the doCreateEmptyVa_list private and then when you call your function call

CCMenuItemToggle::initWithTarget( this, menu_selector(GOSound::toggleButtonCallback), NULL, CreateEmptyVa_list() );

You cannot pass NULL as the fourth argument of your function. That function requires a va_list argument. NULL in general case is not a valid initializer for a va_list object. So, the answer to your question is: it is not possible.

How you should deal with it depends on what you are trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM