问题描述
我用C ++编写代码,并且有几个关于省略号的问题:
I am coding in C++ and have a few questions regarding the ellipsis:
-
类指针到省略号?
Is it possible to pass in class or class pointer into the ellipsis?
基本上我想做的是在 char *
和 class
。我现在使用省略号,并试图找出如何传入类。如果省略号在这里不适用,有什么可用的选项?
Basically what I want to do is to pass in variable number of arguments in the type of char*
and class
. I am using ellipsis currently and trying to figure out how to pass in the class. If ellipsis isn't applicable over here, what are the options available?
我想让用户直接使用 func(params 1, params2,...)
,在将向量或数组作为参数传递给函数之前,首先不必显式地将参数赋值为向量或数组。
I want to let the user to directly call the function using func( params 1, params2, ...)
without explicitly assigning the params into a vector or array first before passing the vector or array as argument to the function.
推荐答案
您应该考虑使用可变函数(C风格)是一个危险的缺陷。
如果传递给函数的对象不匹配,类型等待,或者如果你没有放置正确的参数数量,那么你在运行时基本上会暴力崩溃。
You should consider that using variadic functions (C-style) is a dangerous flaw.If the objects passed to the function mismatch the type awaited, or if you don't put the exact number of parameters awaited, then you basically have a violent crash at runtime.
在Bjarne Stroustrup C ++ In Depth系列 - C ++编码标准 - 101 Herb Sutter和Andrei Alexandrescu的规则,指南和最佳实践,第98章:不要使用varargs (省略号)
In Bjarne Stroustrup C++ In Depth Series - C++ Coding Standards - 101 Rules, Guidelines, And Best Practices by Herb Sutter and Andrei Alexandrescu, chapter 98: Don't use varargs (ellipsis)
我深深地订阅了@ tenfour的建议:
I deeply subscribe to @tenfour's proposal:
- 使用包含所有参数的
std :: vector
。
- use an
std::vector
that contains all your parameters.
这篇关于传递不同类型的参数的可变数目 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!