简体   繁体   English

如何在构造函数中将对象数组初始化为相同的值?

[英]How can I initialize an array of objects to the same values in my constructor?

I have the following lines in my code: 我的代码中有以下几行:

#define NUM_THREADS 8 // NUM_THREADS is variable

myClass myVar[NUM_THREADS] {{&A,&x}};

A and x are just variables that are used in the myClass constructor to setup the size of some variables of the myVar objects. A和x只是myClass构造函数中用于设置myVar对象的某些变量大小的变量。 However, the values of A and x aren't known until run time. 但是,A和x的值直到运行时才知道。

Since NUM_THREADS is variable, what I want is for every object (ie myVar[NUM_THREADS-1:0] to get the same values sent in for the constructor, without having to manually type out the following.: 由于NUM_THREADS是可变的,我想要的是每个对象(即myVar [NUM_THREADS-1:0]获取为构造函数发送的相同值,而不必手动键入以下内容:

myClass myVar[NUM_THREADS] {{&A,&x},{&A,&x},{&A,&x},{&A,&x},{&A,&x},{&A,&x},{&A,&x},{&A,&x}};

Here's what the constructor looks like: 这是构造函数的样子:

myClass::myClass(Aclass *A_orig, Xclass *x_orig) { 
  A_new = *A_orig;
  x_new = *x_orig;
}

You can use std::index_sequence to generate numbers from 0 to N in a parameter pack. 您可以使用std::index_sequence在参数包中生成从0N数字。 This is how I'd do it: 我就是这样做的:

MyClass&& make_my_class(std::size_t, MyClass&& m) { return std::move(m); }

template<std::size_t... S>
void makeThreads(std::index_sequence<S...>) {
    std::array<MyClass, S> myVar{make_my_class(S, {&A,&x})...};
}

template<std::size_t N>
void makeThreads() {
    makeThreads(std::make_index_sequence<N>{});
}

Now, you can use it like that: 现在,您可以像这样使用它:

constexpr std::size_t numberOfThreads = 8;

makeThreads<numberOfThreads>();

The advantage of this solution is that the resulting code is exactly the same as if you'd wrote it by hand. 这个解决方案的优点是生成的代码与您手动编写的代码完全相同。

One disadvantage of this solution is that you have to know the number of thread you want at compile-time, whereas the solution using vectors is allowed to determine the size at runtime. 此解决方案的一个缺点是您必须在编译时知道所需的线程数,而允许使用向量的解决方案在运行时确定大小。 It make sense, as you ask the compiler to fill the braces for you. 这是有道理的,因为你要求编译器为你填充大括号。 For the compiler to do it, it has to know the number of MyClass you want it to repeat in the array initialization. 对于编译器来说,它必须知道您希望它在数组初始化中重复的MyClass的数量。

我建议你使用std::vector

std::vector<MyClass> v1(20, MyClass(&A,&x)); // Initialize a vector with 20 instances of MyClass.

Create the array and construct each of the objects in a loop. 创建数组并在循环中构造每个对象。

myClass myVar[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; ++i) myVar[i] = myClass(A, x);

You are default constructing objects first, but that isn't necessarily a problem. 您首先默认构造对象,但这不一定是个问题。 A better solution might be to provide setters for both of those fields and set them in the loop if you don't mind them being mutable . 一个更好的解决方案可能是为这两个字段提供setter, 如果你不介意它们是可变的 ,可以在循环中设置它们。


Really, though, you should probably be using std::vector or std::array if you support them. 但实际上,如果你支持它们,你应该使用std::vectorstd::array

I do not think it is possible without using a dedicated function. 如果不使用专用功能,我认为不可能。 However you can test with std::fill or std::fill_n from the algorithm header. 但是,您可以使用algorithm标头中的std::fillstd::fill_n进行测试。

Here is a sample: 这是一个示例:

 #include <iostream> #include <algorithm> class AClass { public: AClass() {} AClass(int a, int b) : a(a), b(b) {} int getA() { return a; } int getB() { return b; } private: int a; int b; }; int main() { AClass aclass[8]; std::fill_n(aclass, 8, AClass(1, 2)); for (int i = 0; i < 8; ++i) { std::cout << aclass[i].getA() << aclass[i].getB() << "\\n"; } std::cin.get(); return 0; } 

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

相关问题 如何在构造函数中初始化具有相同值的对象数组 - How to initialize array of objects with the same value in constructor 如何在构造函数中初始化对象数组? - How to initialize array of objects in constructor? 如何初始化对象数组? - How can I initialize an array of objects? 在构造函数中初始化对象数组 - Initialize array of objects in constructor 如何在 c ++ 的构造函数中初始化对象数组 - how to initialize an array of objects in a constructor in c ++ 如何使用给定构造函数的大小初始化 C++ 中的数组? - How can I initialize an array in C++ with a size that is given to the constructor? "如何在没有默认构造函数的情况下初始化类的 std::array?" - How can I initialize an std::array of a class without a default constructor? 我如何为 c++ 中分配的动态对象数组中的所有对象调用参数化构造函数? - How can i call the parameterized constructor for all objects in my dynamic array of objects on allocation in c++? 如何使用一些参数初始化动态分配数组中的对象 - How can I initialize objects in a dynamically allocated array with some arguments 如何在构造函数中初始化char数组? - How can I initialize char arrays in a constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM