How to initialize C-style char array and int array via member initialization list through constructor?

  arrays, c++, constructor, member-initialization, string

Code-1

#include <iostream>
#include <cstring>
class A
{
private:
    int p[5];
    char str[20];
public:
    A(int *q, char *s)
    {
        for(int i=0; i<=4; i++)
        {
            p[i]=*q;
            q++;
        }
        strcpy(str,s);
    }

};

int main()
{
    int r[5]={2, 3, 5, 7, 11};
    char ch[]="bonaparte";
    A a1(r, ch);
    
    return 0;
}

Output ( Runs smoothly but just gives warning )

Clang-Tidy: Constructor does not initialize 
these fields: p, str

Why this warning is coming. I know that I am assigning in constructor not initializing but When I create simple class which just have int type variable and If I assign that in this same way it didn’t give such warning ?

Code-2

#include <iostream>
#include <cstring>
class A
{
private:
    int p[5];
    char str[20];
public:
    A(int *q, char *s):  // just not getting how we can do this initialization
    {

    }
};

int main()
{
    int r[5]={2, 3, 5, 7, 11};
    char ch[]="bonaparte";
    A a1(r, ch);
   
    return 0;
}

Is there any way to initialize int type or C-style char array via member initialization list through constructor.

I know that I can replace char array with string but I want to know a way for C-style char array.

Source: Windows Questions C++

LEAVE A COMMENT