简体   繁体   English

我应该如何将NULL传递给va_list函数参数?

[英]How should I pass NULL to the va_list function parameter?

I wanna pass NULL to the 4th param of the following function: 我想将NULL传递给以下函数的第4个参数:

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). 我在XCode(clang3.1)中构建它时没关系。 But when I port the code to android ndk (g++4.7), it fails to compile: 但是当我将代码移植到android ndk(g ++ 4.7)时,它无法编译:

no viable conversion from 'int' to 'va_list' (aka '__builtin_va_list') 没有可行的从'int'转换为'va_list'(又名'__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. 我假设如果你只使用空的va_list而不是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; 以下代码将通过Visual Studio中的运行时错误; however, it works fine with g++. 但是,它适用于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。

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 将doCreateEmptyVa_list设为私有,然后在调用函数调用时

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

You cannot pass NULL as the fourth argument of your function. 您不能将NULL作为函数的第四个参数传递。 That function requires a va_list argument. 该函数需要一个va_list参数。 NULL in general case is not a valid initializer for a va_list object. 通常情况下, NULL不是va_list对象的有效初始值设定项。 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. 你应该如何处理它取决于你想要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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