[comp.lang.c++] Default initializations for class constructors

schmidt@bonnie.ics.uci.edu (Douglas C. Schmidt) (09/10/88)

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?

The following is a short example illustrating my question:
----------------------------------------
#include <stream.h>

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

--
schmidt@bonnie.ics.uci.edu (ARPA)

bs@alice.UUCP (Bjarne Stroustrup) (09/10/88)

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 <stream.h>
 > 
 > 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

vkar@ihlpa.ATT.COM (Thayalan) (09/13/88)

In article <8184@alice.UUCP>, bs@alice.UUCP (Bjarne Stroustrup) writes:
> 
> 
> 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





The Zortech C++ compiler on my PC handles this correctly.
It gives a error, stating that it cannot resolve which
constructor for the class foo to use in the derived class bar.

k. Thayalan