TITLE: initializer and no arg constructor syntax (Newsgroups: comp.lang.c++.moderated, 14 May 97) MARSHALL: todd alden marshall > > Why does a global declaration for an automatic class instance with a > constructor that takes no arguments need to be done with no parenthesis? > class foo fooobj(); //does not work, the linker doesn't see it, extern > class foo fooobj; or fooobj(); does not work. The only thing that works > is 'class foo fooobj;', and the constructor for class foo takes no > paramaters. How come () causes the problem? (bc4.52) CLAMAGE: Steve Clamage The syntax for declaring an object of type T looks approximately like this: T objectname initializer ; where the initializer is not always required. Initializers have two forms (apart from special syntax for "aggregates", which we'll ignore for this discussion): = value (value1, ... valueN) At least one value must be present. For a single value you can use either form: T x (1) ; T x = 1 ; If the initializer (constructor in this case) requires more than one value, you must use the parentheses form: T x (1, 2, 3) ; (I don't normally use all these blanks in my code. I'm using them here to emphasize the syntax elements.) To create an object x with no initialization, you cannot write T x = ; nor can you write T x () ; You specify no initialization (or default intialization) by omitting the entire intializer: T x ; The syntax T x () ; always has another meaning, inherited from C: It declares x as function with no parameters returning a value of type T. For example, you would expect int f(); to mean 'f' was an uninitialized int variable!