199k views
0 votes
Consider this constructor:

Foo(int a, double d) {
alpha = a;
delta = d;
}
Which answer is equivalent code, but using a member initialization list?

a) Foo(int a, double d) : alpha(a), delta(d) {}
b) Foo(int a, double d) { alpha = a; delta = d; }
c) Foo(int a, double d) : { alpha = a; delta = d; }
d) Foo : alpha(0), delta(0) {}

1 Answer

1 vote

Final answer:

The correct answer is option a) Foo(int a, double d) : alpha(a), delta(d) {}, which uses a member initialization list to initialize the variables alpha and delta.

Step-by-step explanation:

The correct answer is a) Foo(int a, double d) : alpha(a), delta(d) {}. This option uses a member initialization list to initialize the variables alpha and delta with the values of the parameters a and d, respectively. The member initialization list is more efficient and preferred when initializing class member variables.

In contrast, option b) Foo(int a, double d) { alpha = a; delta = d; } initializes the variables alpha and delta inside the constructor body using assignment statements, which is less efficient.

Option c) Foo(int a, double d) : { alpha = a; delta = d; } is not a valid syntax. Option d) Foo : alpha(0), delta(0) {} initializes the variables alpha and delta with the value 0, which is different from the given constructor's behavior.

User Milligran
by
8.2k points