Home > Blogs > 30 C++ Tips in 30 Days: Tip # 14 the corrext syntax for local object instantiation

30 C++ Tips in 30 Days: Tip # 14 the corrext syntax for local object instantiation

By  Jan 14, 2008

Topics: Programming, C/C++

The syntax for instantiating a local automatic object is almost identical to that of a function declaration. This similarity is common source of mistakes.

Consider the following pair of declarations:

string g();
string h;

g is a function that takes no arguments and returns a string by value. In spite of the superficial syntactic similarity, h denotes a different entity, namely a string object whose name is h.

The same confusion occurs with static declarations:

static string g();
static string h;

In this example, g is a static function that returns a (nonstatic) string object by value. This function has internal linkage, meaning it's visible only from the translation unit that declares that function. h in this example is a static string object.

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.