How to invoke hi::hi(int,int)
?
If hi
is actually an aggregate type and/or you're using C++11, you can built it simply with:
hello = new hi[5]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
If hi
is not an aggregate, per se you cannot. But with a bit of improvement, we can achieve it.
- Declare a private default constructor:
This gives:
class hi {
hi();
public:
hi(int a, int b){};
};
Thhe idea is to provide a default constructor for the standard container to find, even though std::is_default_constructible_v<hi>
is false
. Obviously, any actually attempt to default construct an hi
will end in a compilation failure.
- Use an
std::array
or a std::vector
instead of a C array:
This gives:
std::vector<hi> his;
- Use
std::generate_n
to construct your objects:
This gives:
his.reserve(number_of_instances);
std::generate_n(std::back_inserter(his), number_of_instances, [](){ return hi{0, 0}; });
Note though, this vector as a vector of non-default constructibe type is uncomplete, you'll be unable to use all of its features.
Demo
Another approach would be to reserve some memory as arrays of unsigned char
and construct hi
s instances in it with placement new
s.