Q:

The following initializer is in error. Identify and fix the problem

0

The following initializer is in error. Identify and fix the problem.

struct X { X (int i, int j): base(i), rem(base % j) { } int rem, base;
};

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The order of member initialization is the same with the order they appear in the class definition. Since `rem` appears first, it will be initialized first. But the value of `base` is undefined when `rem` is initialized, thus the value of `rem` is undefined. To fix the problem, we can either switch the order of definitions of `rem` and `base` or we can use the constructor parameters `i` and `j` direct initialize `rem(i % j)`.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now