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));   } 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: