私のコース ノートには、これら 2 つの例が示されています。どうやら最初のものは許可されていません。スタックに割り当てることができない技術的な理由はありますか? それとも、これは C++ 標準ですか?
// Constructor may force dynamic allocation when initializating an array of objects.
Complex ac[10]; // complex array initialized to 0.0
for ( int i = 0; i < 10; i += 1 ) {
ac[i] = (Complex){ i, 2.0 } // disallowed
}
// MUST USE DYNAMIC ALLOCATION
Complex *ap[10]; // array of complex pointers
for ( int i = 0; i < 10; i += 1 ) {
ap[i] = new Complex( i, 2.0 ); // allowed
}