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: All
Sep 16, 1999

Initializing Array Class Members

You cannot initialize an array member in a member-initialization list of a class. For this reason, the following code will not compile:
 
  class A
  {
  private:
    char buff[100]; 
  public:
    A::A() : buff("")   //ill-formed
    {}
  };
 
The following forms won't compile either:
 
  A::A() : buff('\0')  {}  //ill-formed
  A::A() : buff(NULL)  {}  //ill-formed
 
Instead, you should initialize arrays inside the constructor body, as follows:
   
  A::A() 
  { 
    memset(buff, '\0', sizeof(buff)); 
  }
 
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