简体   繁体   English

C ++中可变参数函数的问题

[英]Problem with variable argument function in C++

I'm trying to create a variable length function (obviously, heh) in C++, and what I have right now works, but only for the first argument. 我正在尝试在C ++中创建一个可变长度的函数(显然是heh),而我现在拥有的东西可以正常工作,但仅适用于第一个参数。 If someone could please let me know how to get this working with all the arguments that are passed, I would really appreciate it. 如果有人可以让我知道如何处理所有传递的参数,我将非常感激。

Code: 码:

void udStaticObject::accept( udObjectVisitor *visitor, ... )
{
    va_list marker;
    udObjectVisitor *i = visitor;
    va_start( marker, visitor );
    while( 1 )
    {
        i->visit_staticObject( this );
//the if here will always go to the break immediately, allowing only 
//one argument to be used
        if( ( i = va_arg( marker, udObjectVisitor* ) ) )
            break;
    }
    va_end( marker );
}

Based on my past posts, and any help posts I make in general, there is probably some information that I did not provide that you will need to know to help. 根据我过去的帖子以及我通常发布的所有帮助帖子,可能有一些我没有提供的信息,您需要知道这些信息来提供帮助。 I apologize in advance if I forgot anything, and please let me know what you need to know so I can provide the information. 如果我忘了任何事情,我事先表示歉意,请告诉我您需要了解的内容,以便我们提供相关信息。

If you use a variadic function, you need some way to tell the function how many arguments were passed. 如果使用可变参数函数,则需要某种方法来告诉该函数传递了多少个参数。 For example, printf() and friends take a formatting string that contains format specifiers for each of the passed arguments, and they count the number of format specifiers to determine how many arguments were passed. 例如, printf()和朋友使用一个格式化字符串,其中包含每个传递的参数的格式说明符,并且他们对格式说明符的数量进行计数以确定要传递的参数数量。

When passing a list of pointers, you can accomplish this "more simply" by passing a null pointer as the last argument. 传递指针列表时,可以通过传递空指针作为最后一个参数来“更简单”地完成此操作。 That way, you simply read arguments until you reach the null pointer. 这样,您只需读取参数,直到到达空指针。

However , you should seriously consider not using a variadic function for this. 但是 ,您应该认真考虑不要为此使用可变参数函数。 You can accomplish the same behavior by taking a vector of pointers as a parameter and iterating over the contents of that vector. 通过将指针向量作为参数并遍历该向量的内容,可以实现相同的行为。 There are a number of reasons why this is superior to using a variadic function: 有许多原因使它优于使用可变参数函数:

  • Variadic functions have absolutely no type safety. 可变参数函数绝对没有类型安全性。 You lose any and all type information about the arguments when you pass them to a variadic function, so for example, a caller of your function could pass a numeric value instead of a pointer and you'd never be able to tell inside of your function. 当您将参数传递给可变参数函数时,您会丢失有关参数的所有类型信息,因此,例如,函数的调用者可以传递数字值而不是指针,并且您永远无法分辨函数内部。

  • With the variadic solution, the caller must correctly indicate the number of arguments. 使用可变参数解决方案时,调用者必须正确指示参数数量。 If the caller omits the null pointer at the end (or otherwise misinforms your function of how many arguments there are) and you try to read more arguments than were passed, you end up with undefined behavior. 如果调用方在末尾省略了空指针(或以其他方式错误告知您的函数有多少个参数),并且您尝试读取的参数比传递的参数多,那么最终将导致未定义的行为。 Right now you might say "well, that's not hard to forget," but inevitably, someone will forget or screw it up and debugging this sort of issue is a beating. 现在您可能会说“嗯,这不难忘记”,但不可避免地,有人会忘记或搞砸它,调试此类问题真是一跳。

  • The solution taking a vector and iterating over its contents is far simpler to implement, easier to debug, and much more idiomatic in C++. 采用向量并遍历其内容的解决方案在C ++中实现起来要简单得多,调试起来也要方便得多。

Wherever there is an option between using a variadic function and not using a variadic function, you should prefer not to use a variadic function (I'll admit, I have never written a variadic function in any of the C++ code I have written, though I've written a few in C). 在可以使用可变参数函数和不使用可变参数函数之间的任何选项的地方,您都应该不要使用可变参数函数(我承认,我从未用我编写的任何C ++代码编写可变参数函数。我已经用C编写了一些。

Maybe the test is backwards? 也许测试是倒退的? Try this: 尝试这个:

    if( ( i = va_arg( marker, udObjectVisitor* ) ) != NULL )
        break;

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

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