Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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++_Function_Arguments - Fatal编程技术网

C++ C+;中的函数参数数量可变+;

C++ C+;中的函数参数数量可变+;,c++,function,arguments,C++,Function,Arguments,所以,在C中,标准方法是stdarg.h。但我想提出一些类似这样的东西: template<int A> class MyClass { public: MyClass(...) { // fill each value of array with an argument. }; virtual ~MyClass() { }; private: float array[A]; }; 模板 类MyClass{ 公众: MyClass(

所以,在C中,标准方法是stdarg.h。但我想提出一些类似这样的东西:

template<int A>
class MyClass {
public:
    MyClass(...) {
        // fill each value of array with an argument.
    };

    virtual ~MyClass() { };
private:
    float array[A];
};
模板
类MyClass{
公众:
MyClass(…){
//用一个参数填充数组的每个值。
};
虚拟~MyClass(){};
私人:
浮点数组[A];
};
显然,我们的想法不是对每一个可能的论点都有不同的解释。有什么建议,标准方法吗

谢谢

Julian.

在C++11中,您可以为这种场景使用构造函数。允许进行这种类型的初始化:

MyClass<5> x{1,2,3,4,5};
MyClass x{1,2,3,4,5};

尽管您必须定义尺寸不匹配时发生的情况。但是对于这些类型的静态大小的数组,值得一看。当初始值设定项的维度与它们自己的维度不匹配时,它们有一个定义良好的行为。

如果您无法访问C++11初始值设定项列表,您可以只传递一个向量

MyClass(const std::vector& values) {
    //copy values into array
};

MyClass someClass( std::vector<int>(10,4) ); //adds 10x 4 to the vector
MyClass(常量std::向量和值){
//将值复制到数组中
};
MyClass-someClass(std::vector(10,4))//将10 x 4添加到向量

您需要使用
初始值设定项\u列表

explicit MyClass(std::initializer_list<T> list_args){
    // fill each value of array with an argument.
};
显式MyClass(std::初始值设定项\u列表\u参数){ //用一个参数填充数组的每个值。 };
传递容器或使用std::initialize\u列表(如果有)。