Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将参数列表用于C++;_C++_Templates - Fatal编程技术网

C++ 将参数列表用于C++;

C++ 将参数列表用于C++;,c++,templates,C++,Templates,这就是我想要做的——我有一个有几个构造函数的类a。我不想更改类A中的任何内容。我希望有一个新的类B,并且A应该是B的元素,并且有一个镜像A的构造函数 class A{ public: A(type1 arg1); A(type2 arg1, type3 arg2); } 我知道我能做到 class B{ A *a; public: B(type1 arg1) { a = new A(arg1); //some other stuff here

这就是我想要做的——我有一个有几个构造函数的类a。我不想更改类A中的任何内容。我希望有一个新的类B,并且A应该是B的元素,并且有一个镜像A的构造函数

class A{
public:
   A(type1 arg1);
   A(type2 arg1, type3 arg2); 
}
我知道我能做到

class B{
A *a;
public:
   B(type1 arg1) {
      a = new A(arg1);
      //some other stuff here
   }
   B(type2 arg1, type3 arg2) {
      a = new A(arg1, arg2);
      //some other stuff here, same as the first constructor
   }
}
有办法做到这一点吗

class B{
A *a;
public:
   B(list_of_args) {
      a = new A(list_of_args);
      //some other stuff here
   }
}

我不想扩展A。我只是想找到一种方法来获取传递给B的参数列表,并用这个函数调用A的构造函数。(无需更改A)。

使用C++11和可变模板,轻松:

class B {
    A a;

public:
    template <typename... Args>
    B(Args&&... args)
    : a(std::forward<Args>(args)...)
    { }
};
B类{
A A;
公众:
模板
B(参数和参数)
:a(标准::转发(参数)…)
{ }
};
如果没有可变模板,您可以为不同数量的参数添加一组模板构造函数,但这非常难看。如果所有参数都不是引用,那就没那么糟糕了

模板
B(常数A1和A1):a(A1){}
模板
B(常数A1和A1,常数A2和A2):a(A1,A2){}
//等等。
但实际上,您需要分别处理
A1&
const A1&
。。。因此,对于
N
参数,您需要
2^N
构造函数。你可以看到为什么人们对C++11感到兴奋


(请注意,在代码中,您有一个类型为
a
的类成员,但在构造函数示例中,您试图创建一个
a*
)。

您创建了一个模板构造函数,并使用可变模板参数:

class B
{
public:
   template<typename... ArgTypes>
   B(ArgTypes... list_of_args) : a(list_of_args...)
   {
     //some other stuff here
   }
private:
  A a;
}

我正在根据您的需求调整Barry的解决方案:

class A{
public:
   A(type1 arg1);
   A(type2 arg1, type3 arg2); 
}



class B{
A *a;
public:
   template <typename... Args>
   B(Args&&... args) {
      a = new A(std::forward<Args>(args)...);
      //some other stuff here
   }
}

有什么特别的理由不转发参数列表吗?撇开效率不谈,例如,如果其中一个参数是仅移动类型,则此操作将失败。@Ben除了不考虑这种情况之外,没有其他原因。在中编辑。虽然这将起作用,但如果编译器没有内联构造函数,它也可能会创建代码膨胀。考虑<代码> > <代码>的唯一构造函数是<代码> a(int)< /代码>,程序的不同部分称之为“代码> b(1),<代码> B(1U)< /代码>,<代码> B(1),<代码> B(1 .f),<代码> B(1S)< /代码>…整个问题都有点代码味道,真的。很少有情况下,这种[OP想要的]设计不会很糟糕。谢谢@Deduplicator,它会解决的。每次都是这样!虽然这个问题可以从技术上得到回答,但更有趣的问题是关于设计。我发现必须将参数一般地转发到一个众所周知的类型,而不是保存类型,这不是一个很好的理由。
template<typename... ArgTypes>
B(ArgTypes&&... list_of_args) : a(std::forward<ArgTypes>(list_of_args)...)
{
  // do something here
}
class A{
public:
   A(type1 arg1);
   A(type2 arg1, type3 arg2); 
}



class B{
A *a;
public:
   template <typename... Args>
   B(Args&&... args) {
      a = new A(std::forward<Args>(args)...);
      //some other stuff here
   }
}
class B{
A *a;
public:
   void initialize(A* a) {
      this->a = a;
      //some other stuff here
   }

   B(type1 arg1) {
      initialize(new A(arg1));
   }

   B(type2 arg1, type3 arg2) {
      initialize(new A(arg1, arg2));
   }
}