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: Intermediate
Jan 18, 2000

Understanding Default Initialization

A POD (plain old data) type is a union, struct or class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. To default-initialize an object of type T means the following: if T is a non-POD class type, the default constructor for T is called. If T is an array type, each element is default-initialized. Otherwise, the storage for the object is zero-initialized.

An object whose initializer is an empty set of parentheses, i.e., (), shall be default-initialized. For example:

 

  struct Date // POD type
  {
    int day;
    int month;
    int year;
  };
  Date d = Date();  // default initialize d
Because d is of a POD type, the explicit default initialization of d initializes all of its members to 0. However, the default initialization rule was added recently to C++, so at present, very few compilers implement it. Therefore, you should use an explicit initialization list to ensure that d is zero-initialized:
 

  Date d = {0}; // guarantee zero initialization
Danny Kalev
 
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