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.