Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!ucsd!rutgers!bellcore!faline!thumper!ulysses!andante!alice!bs From: bs@alice.UUCP (Bjarne Stroustrup) Newsgroups: comp.lang.c++ Subject: Re: Default initializations for class constructors Summary: a bug Keywords: c++ Message-ID: <8184@alice.UUCP> Date: 10 Sep 88 01:31:19 GMT References: <684@paris.ICS.UCI.EDU> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 58 bonnie.ics.uci.edu University of California, Irvine - Dept. of ICS) > Hi, > Can someone please enlighten me as to the correct behavior of C++ > when class constructors with different argument types are all declared > to have default parameter initializers? My basic question is, which > of the constructors should be selected if the type is not clear from > the definition of a class object? G++ appears to use the first > lexically presented constructor as the default, is this specified, or > is the behavior undefined in the language? No, the implicit call of (some) foo::foo() from bar::bar() is ambiguous and therefore clearly an error. Cfront gets it wrong too, though. Sorry. - Bjarne > #include > > class foo { > int i; > public: > foo(double j = 10.5) { > i = int(j) * 5; > cout << "double constructor\n"; > } > foo(int j = 10) { > i = j; > cout << "int constructor\n"; > } > int Tell() { > return i; > } > }; > > class bar : public foo { > int i; > public: > bar(void) { > i = foo::Tell(); > } > int Tell(void) { > return i; > } > }; > > main() { > bar Foo; > cout << Foo.Tell() << "\n"; > // G++ prints out 50 at this point, indicating that it called > // the double constructor as default. > } > thanks for any help on this matter, > Doug Schmidt