devxlogo

Initialization of Built-In Types

Initialization of Built-In Types

Question:
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?

Answer:
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.

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist