Warning: will be initialized after [- writer]

Today, I found a warning when I was writing a C + + program. After thinking about it, I found the answer on the Internet. Well, there is something wrong with the order

class A
{
public:
    A(int a, int b);

private:
    int two;
    int one;
};

A::A(int a, int b):one(a),two(b)
{

};

In the G + + compiler, there will be warnings when compiling:

warning: 'A::one' will be initialized after [-Wreorder]

The main problem is the order (pay attention to the order of one and two), that is, the order of members in the initialization list and the order of members in the class must be consistent, so that there will be no warning again. Change the above code:

class A
{
public:
    A(int a, int b);

private:
    int one;
    int two;
};

A::A(int a, int b):one(a),two(b)
{

};

Similar Posts: