error in new'd array of objects

Paul Kendall paul@orion.co.nz
Mon Nov 17 20:46:00 GMT 1997


Hi,
The following code fails on the line a=new A[2](1,false);
becuase the compiler is trying to call a contructor
with a bool parameter.  Which does not exist.  Some
folks say that this construct is invalid C++ (yes it is),
BUT it is a GNU extension according to cp/parse.y
and it fails.  With the -pedantic option a warning 
"initialization in array new" is given (which is correct).

#include <stdio.h>

class A {
public:
	A(bool b) { printf("dooh\n"); }
	A(int a, bool b) { printf("cool\n"); }
};

main() {
	A* a;
	a = new A[2](1,false);
}

this code prints
dooh
dooh
and should print
cool
cool

If I change the line to a = new A[2] = { A(1,false), A(1,false) };
I get
cool
cool
which is correct.  With -pedantic I get the follwoing warning
"ANSI C++ forbids initialization of new expression with '='"
"initialization in array new"
whereas the previous line just gave the second warning.  I
hope this helps someone fine the error.  Either by removing
the ability to call a non-default constructor on new array
or by fixing it so that it works.

Cheers,
Paul Kendall
Product Development Manager
Orion Systems New Zealand Ltd




More information about the Gcc-bugs mailing list