From: Alf P. Steinbach on
* Martin B., on 11.06.2010 13:34:
>
> The constructing order of the specific constructor is relevant, and
> *not* the declaration order of the members. (Or so I thought.)

This will probably trigger a flood of like responses (one case where a mod
factoid could be beneficial?), but in case it doesn't trigger that flood:

no, the memory initializer list order does not matter, it's only the declaration
order that matters.

{ Yes, a mod factoid could certainly have been beneficial. :) -mod/sk }


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Frank Bergemann on
PolarPeter wrote:
> One of many questions I typically ask candidates who come to interview
> with me is this one:
>
> class A
> {
> public:
> A() : y(2), x(y * 2) {}
> ~A() {}
>
> private:
> int x;
> int y;
> };
>
> int main()
> {
> A a;
> return 0;
> }
>
> I ask them what the value of a.x and a.y will be after 'a' has been
> constructed.
>
> [...]
>
> I'd like to know what you think - is it reasonable to expect
> candidates who claim they have 10 years+ 'advanced' C++ experience to
> know this?
>

I'd expect the candidate to say to you:
"I see you have spent some efforts and not just quoted the example of
Scott Meyer's 'Effective C++', Item #13 'List members in an
initialization list in the order in which they are declared'."

Likely you then would smile at each other :-)

cheers,
Frank

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Morley on
> I think it's a fair and reasonable question. For those who don't
> get the correct answer a follow up question might be why is
> x == 0 and y == 2.

Go on, impress. Why?


#include <stdio.h>
class A {
public:
A() : y(2), x(y * 2) {}
~A() {}
void print() {printf("x=%d, y=%d\n",x,y);}
private:
int x;
int y;
};

void testa() {
A a;
a.print();
}
void foo() {
int z=5;
printf("%d\n",z); // ensure compiler doesn't optimise z away
}
int main() {
foo();
testa();
return 0;
}

Compiled on VS2008 for win 32 console. Inline expansion set to keyword only
(so we guarantee function calls & stack frames...)
/O2 /Ob1 /Oi /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D
"UNICODE" /FD /EHsc /MD /Gy /Fo"Release\\" /Fd"Release\vc90.pdb" /W3 /nologo
/c /Zi /TP /errorReport:prompt

outputs:
5
x=10, y=2

x value is UB. I just seeded the stack in this example with 5 (on this
platform).

Seems a little harsh if they missed the first answer being UB to follow by
asking a broken question... whose answer is "It's still UB."

Chris



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: DeMarcus on
On 2010-06-11 03:22, PolarPeter wrote:
> One of many questions I typically ask candidates who come to interview
> with me is this one:
>
> class A
> {
> public:
> A() : y(2), x(y * 2) {}
> ~A() {}
>
> private:
> int x;
> int y;
> };
>
> int main()
> {
> A a;
> return 0;
> }
>
> I ask them what the value of a.x and a.y will be after 'a' has been
> constructed.
>
> Clearly, the issue here is that x is constructed first, and uses the
> uninitialized value of y. However, a lot of candidates miss the order
> issue, and think that a.y is 2, and a.x is 4.
>
> I think this question is pretty fundamental, given how often MILs are
> used. I think people who say they have 10+ years professional
> experience should be able either to see the issue, or at least talk
> through why it works the way it works if they don't initially see it
> but are then shown.
>
> I'd like to know what you think - is it reasonable to expect
> candidates who claim they have 10 years+ 'advanced' C++ experience to
> know this?
>

I would probably expect such person to know that there /is/ an
initialization order mechanism for it. How it works takes no time to
check up.

However, since you ask; the most important thing is the way you present
/yourself/ to a person that actually might be a really skilled
programmer. If you judge him/her by one simple example only, he/she
might think "Will this guy measure me by the number of lines I produce
each day?".

So as an answer to your question; well, yes, you could ask such
question, but make sure you have a bunch of interesting questions or
preferably a nice discussion where you could ask him/her something like
"Tell me one of your most fun aha-experiences during your career". Or
"What in C++0x are you waiting for most?".

If you really want a 10 years+ experienced programmer it's important you
don't make your company look boring.




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin B. on
Daniel Kr�gler wrote:
> On 11 Jun., 13:34, "Martin B." <0xCDCDC...(a)gmx.at> wrote:
>> PolarPeter wrote:
>>> One of many questions I typically ask candidates who come to interview
>>> with me is this one:
>>> class A
>>> {
>>> public:
>>> A() : y(2), x(y * 2) {}
>>> ~A() {}
>>> private:
>>> int x;
>>> int y;
>>> };
>>> int main()
>>> {
>>> A a;
>>> return 0;
>>> }
>>> I ask them what the value of a.x and a.y will be after 'a' has been
>>> constructed.
>>> Clearly, the issue here is that x is constructed first, and uses the
>>> uninitialized value of y. However, a lot of candidates miss the order
>>> issue, and think that a.y is 2, and a.x is 4.
>> Under MSVC 2005 this give the result of y=2 and x=4. (With Language
>> extensions disabled and with /W4 which doesn't give any warnings.)
>>
>> The constructing order of the specific constructor is relevant, and
>> *not* the declaration order of the members. (Or so I thought.)
>
> You are misinformed. It is very important that the rules are as they
> are specified, (...)


Rather I'd say I was misremembering.

Which just goes to show that the important part to remember is: Don't do
this, or as item 47[*] says: "Define and initialize member variables in
the same order." (Which also goes to show, that I could have just looked
it up in the book sitting next to my desk -- if I'd remembered where I
last read about it.)

> On Jun 10, 9:22 pm, PolarPeter <polarpeterb...(a)gmail.com> wrote:
>> I'd like to know what you think - is it reasonable to expect
>> candidates who claim they have 10 years+ 'advanced' C++ experience to
>> know this?

Well I'm a C++ programmer with 5 years experience. Judge yourself how I
handled the question. :-)

cheers,
Martin

[*] Suttner/Alexandrescu, C++ Coding standards


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]