Can Someone help tell me what these warning are?

Google has not helped me figure this one out.

1 Like

Member variables are initialised in declaration order, always.

class Example
{
    int x;
    int y;
public:
    Example(int a, int b) : y(a), x(b) {}
};

This code is misleading as x is initialised before y even though it appears after initialising y in the code.

This can be problematic when one is dependent on the other. For example changing that code to be

y(a), x(y + b)

This is undefined behaviour as y is being read before it’s initialised.

This is why that warning exists and why you should get into the habit of initialising members in declaration order.

3 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms