dude_1967: the initialisation B::j uses B::i not ::i. The use of B:: scope resolution brings class B into scope for the rest of the line, so B::i hides ::i.
Printable View
dude_1967: the initialisation B::j uses B::i not ::i. The use of B:: scope resolution brings class B into scope for the rest of the line, so B::i hides ::i.
Indeed Graham. Thanks.
And ::i is not used in main.
And the answer is... once and for all... 10, 5
Whew that was enough for me.
Chris.
;)
So by using B::j = i + 5, what is actually interpreted by th compiler is:
( with scope B:: ) j = i + 5;
Problem solved.
:D
fun..
now try this nice trick, no debugger/watcher ;)
int a=0;
int b;
b=(1||(++a))+2;
a=?
b=?
;)
OK Bengi I'll bite...
The 1 or'ed with the pre-increment of a is a logical expression (basically 1 || 1). The result of the logical expression is 1, since it is non-zero (yes, this is specified in C as well as C++).
Finally a == 1, b == 3.
Used no compiler or debugger.
Chris
:eek:
a = 0
b = 3
Reasons: Short circuit evaluation.
When the expression
(1 || (++a))
is encountered, the evaluation of the || is done from left to right. Since a '1' is encountered first, this automatically makes the expression true (or 1), regardless of the value of "++a"'. Therefore the compiler skips over the (++a) due to short circuit evaluation.
And of course, 1 + 2 = 3.
Regards,
Paul McKenzie
Thanky Paul,
After filing my answer I ran to the compiler and compiled the code snippet and ran it.
I had unfortunately confused (1 || 0[no need to evaluate]) with (1 && ++0).
Thanks for clearing it up.
The exercise was another neat one.
Chris.
:eek:
Bengi,
Your enthusiasm is pleasant. Nonetheless I guess you had better start another thread since the subject is far from initial purpose of the post.
Please note that you may find compilers (I guess from Borland) where you can choose between complete and normal Boolean evaluation. In this case the result is a==1, b==3 in contrast with the normal result: a==0, b==3.
Keep the enthusiasm!
heh, actually under Borland / Visual C
they give sae result, so i guess other compilers will do the same.
since it boolean rules.