C ++ - an array of object instances (I'm trying to find a solution for compilation)

For example, I have the following C ++ class:

struct A { A(const int value) {} }; 

If I want a single object, I can do this:

 A a = A(3); 

I would build 10 of these objects, I do not know how to create static copies.

 A a[10]; // This won't compile, as struct A constructor needs an argument 

I can use a pointer to A *a , and then create an object one by one, but I'm not sure whether there is a new feature in C++11 , is available, which allows me to do it in a static mode, one shot?

+5
source share
2 answers

Initialization of the list allows you to write

 A a[10]{0,1,2,3,4,5,6,7,8,9}; 

Each element in the list is passed to the constructor A .

Live demo

+7
source

If you want to use the same option for all instances of A , you can use an auxiliary function such as the following:

 #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } value} {} #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } <I ...>, int val) { #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } )] {(I, val) ...}; #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } > (), val); #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } i] .v == #include <array> #include <cstddef> #include <functional> #include <cassert> struct A { A(int value): v{value} { } template<std::size_t... I> static A* create(std::index_sequence<I...>, int val) { return new A[sizeof...(I)]{ (I, val)... }; } template<std::size_t N> static A* create(int val) { return create(std::make_index_sequence<N>(), val); } int v; }; int main() { A *arr = A::create<10>(42); for(int i = 0; i < 10; i++) assert(arr[i].v == 42); delete[] arr; } 

The basic idea is to create a new array of instances N A and use the same parameter val for all of them at the time of construction.

Note

std::make_index_sequence and std::index_sequence are part of the game with C ++ version 14. If you can not use them on the Internet, you can find the implementation of C ++ 11, which will help you to use almost the same code as above:

-1
source

All Articles