Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Jun 26, 2000

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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date