Initializing Array Class Members

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('')  {}  //ill-formed  A::A() : buff(NULL)  {}  //ill-formed 

Instead, you should initialize arrays inside the constructor body, as follows:

     A::A()   {     memset(buff, '', sizeof(buff));   } 
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