Initialization of Built-In Types

Initialization of Built-In Types

What does the declaration int i(0) ; mean? Does C++ have a type ‘int’ constructor? And why doesn’t Visual C++ accept a char* p(0) ; declaration?

This expression creates a variable of type int and initializes it to 0. You can achieve the same effect with the following statement:

int i=0;

Technically, built-in types don’t really have a constructor. The compiler transforms the original expression to a simple initialization expression when the variable is of a built-in type. C++ allows you to use this syntax with built-in types for the sake of templates. Templates take an unspecified object as an argument. They can’t tell in advance whether the variable is a built-in type or a class object. To enable them to treat that argument in a uniform fashion regardless of its type, C++ permits the use of this initialization form for built-in types.

The expression char* p(0) ; is valid and should be accepted by a standard-compliant compiler. However, Visual C++ has a bug.


Share the Post: