Hello, I had a seemingly stupid question to which I could not find the answer either in Google, in MSDN. How to define a local array of objects that would not be constructed by a default constructor?
Suppose we have:

class A { A(){}; A(int i){}; ~A(){}; }; 

And we need to define a local array of objects of class A, say 100 pieces, and they should be constructed by the constructor A (int), with the value of the parameter 5.

 A a[100](5);//не компилируется A a(5)[100];//не компилируется 

Immediately came a crazy idea to do:

 A a[100]; for(int i=0;i<100;i++) { a[i].A::~A(); a[i].A::A(5); } 

But you have to be in the language and some normal way to do this?
Something is not my day today, I edited the old copy, I lost the previous changes. I will write briefly:

 A a[100] = A(1.0); 

This can not be done, because:

8.5.1 Aggregates

1. An aggregate (no. Static data members), no base classes (clause 10), and no virtual functions (10.3).
2. When an aggregate is entered, it can be defined as the initializer.

The fact that gcc compiles in this situation should not be compiled according to the standard.

    3 answers 3

    Arrays of objects on the stack are not the best idea.

    The correct way to write what you want is to use std :: vector .

     std::vector<A> a(100, A(5)); 
    • This moment is interesting to me for research purposes, not in practical terms. It turns out that in the language there is no possibility to define an array of non-default designed objects on the stack? - Anton Dmitriev
    • C ++ itself is not the best idea. Many programs on it resemble a statement: first we create problems for ourselves, and then we heroically overcome them - avp
    • @avp, the same can be said about some aspects of programming in C. - skegg
    • @mikillskegg is definitely the case. And what do you want from a macro assembler with types (this is about C)? - avp
    • I want the possibility of overloading functions. C is quite realistic to do. And it would be very convenient. And you can also have templates. It would be quite good. - skegg

    For example:

     A a[100] = A(1.0); 

    Updated from the comment .

     class A { public: int z; A(int x) { z = x; }; }; int main() { A a[3] = A(6); } 

    Everything is working. Generally speaking, it seems to be a normal constructor call 3 times.

    • You can not do it this way. - Anton Dmitriev
    • Moved in response. - AntonS
    • What is a compiler, and, most importantly, what language? As far as I know, in C ++ you cannot initialize an array with an object. - Anton Dmitriev
    • compiler Hz, language s ++. An array of objects cannot be initialized, of course, but the constructor works here. Those. generally speaking this record is similar to the following: A a [0] = A (6); A a [1] = A (6); A a [2] = A (6); - AntonS
    • Apparently, the specificity of the compiler, VC ++ does not compile such things ... - Anton Dmitriev

    For an array of not very large size, you can do this:

     A aa[3] = {{5}, {5}, {5}}; 

    Unfortunately, the C ++ preprocessor is rather primitive, otherwise it would be possible to write a macro that creates such repetitions automatically. For example, the NASM assembler preprocessor allows this.

    • yes, MASM is also a good preprocessor, similar things can be done without problems - Anton Dmitriev