问题描述
亲爱的,
如何生成构造函数以获取可变数量的
参数。升序元组是否适用于此?
我知道有一种方法可以使函数采用可变数量的
参数,但不建议这样做。如果我找不到另一种方式,
我会申请那个。
问候
Dear all,
How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?
I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.
Regards
推荐答案
有通常的添加方式......
我不知道为什么不推荐它,但是你可以随时使用
列表。
偶数和STL :: list,包含如下结构:
struct PARAMETER {
PARAM_TYPE类型;
void * data;
};
There is the usual way of adding ...
I don''t know why it is not recommended, but you can always use
something of a list.
Even and STL::list, that contains structures like this:
struct PARAMETER {
PARAM_TYPE type;
void *data;
};
谢谢,但你能举个例子说明这个结构吗?我是一个有点慢学习的孩子;)
Thank you but Can you give an example with this structure? I am a
little bit slow learner ;)
你不能使用boost :: tuple,因为元组仍然需要知道
有多少参数。可能有一些复杂的方法使用
模板扣除来解决这个问题,但它可能比它的价值更麻烦。
有两种推荐的方法可以达到你想要的效果。第一个
只是简单地重载构造函数,以便为每个可能的数量和类型的参数定义一个
构造函数
to使用。第二个是传递一个标准库的容器
(例如,std :: list,std :: vector等)作为参数,并且具有那个
容器包括所有参数。
祝你好运,
Tom
You can''t use boost::tuple, because the tuple still needs to know how
many arguments there are. There may be some complicated way of using
template deduction to get around that problem, but it would probably be
more trouble than it''s worth.
There are two recommended ways to accomplish what you want. The first
is to simply overload the constructor so that you have defined a
constructor for every possible number and type of arguments you intend
to use. The second is to pass one of the standard library''s containers
(e.g., std::list, std::vector, etc) as as argument, and have that
container include all the arguments.
Best regards,
Tom
这篇关于构造函数的变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!