metz@iamsm.iam.unibe.ch (02/12/90)
>X-Mailer: ELM [version 2.2 PL16] Consider the following program ------------------------------------- #include <stream.h> class A { private: int a; public: A() { cout << "A()\n"; a = 1; } A(int i) { cout << "A(int i) " << i << "\n"; a = i; } ~A() { cout << "~A() " << a << "\n"; } }; int main() { A a; A b[3] = {4, 5}; // array has 3 elements, but only 2 initilizers ! } ------------------------------------- According to C++ 2.0 Ref. Man. p. 81: "Arrays of objects of a class with constructors use constructors in the initilization just like individual objects. If there are fewer objects in the list than elements in the array, the default constructor is used." Here is what I get as output: A() A(int i) 4 A(int i) 5 ~A() 0 ~A() 5 ~A() 4 ~A() 1 NO CONSTRUCTOR CALLED AT ALL!!! The correct output should be A() A(int i) 4 A(int i) 5 A() ~A() 1 ~A() 5 ~A() 4 ~A() 1 Compiler: g++ version 1.36.4 (based on GCC 1.36.93) Lib: libg++ 1.36.2 Igor Metz X400: metz@iam.unibe.ch Institut fuer Informatik ARPA: metz%iam.unibe.ch@relay.cs.net und angewandte Mathematik UUCP: ..!uunet!mcsun!iam.unibe.ch!metz Universitaet Bern Phone: (0041) 31 65 49 90 Switzerland Fax: (0041) 31 65 39 65