[ Team LiB ] Previous Section Next Section

Gotcha #19: Function/Object Ambiguity

A default object initialization should not be specified with an empty initialization list, as this is interpreted as a function declaration.



String s( "Semantics, not Syntax!" ); // explicit initializer 


String t;  // default initialization


String x(); // a function declaration


This is an inherent ambiguity in the C++ language. Effectively, the language standard has "tossed a coin" and decided that x is a function declaration. Note that this ambiguity does not apply to new-expressions:



String *sp1 = new String();  // no ambiguity here . . . 


String *sp2 = new String;  // same as this


However, the second form is preferable, because it's more common and is orthogonal with the object declaration.

    [ Team LiB ] Previous Section Next Section