|
-
June 27th, 2002, 09:01 AM
#1
Exercise
Don't use the compiler. Try to predict the behavior.
Code:
#include <iostream>
using namespace std;
class B
{
public:
static int i;
static int j;
};
static int i = 3;
int B::j = i + 5;
int B::i = 5;
int main()
{
cout << "j == " << B::j << endl
<< "i == " << B::i << endl;
return 0;
}
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
June 27th, 2002, 09:15 AM
#2
Fine, I'll bite.
j == 8
i == 5
Although I suppose this is not the answer, otherwise this would be a very dull post. What's the real answer?
Jeff
-
June 27th, 2002, 09:59 AM
#3
I'd have the same answer as jfaust
Martin Breton
3D vision software developer and system integrator.
-
June 27th, 2002, 10:07 AM
#4
Originally posted by proxima centaur
I'd have the same answer as jfaust
It is not the right answer. I also have been misled by that code.
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
June 27th, 2002, 10:10 AM
#5
Depends whether the line "int B::j = i + 5;" indulges in some form of Koenig lookup or similar. If not, B::j == 8, if it does, then all bets are off, since B::i has no value (or is it 0?) at the time of the statement.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 27th, 2002, 10:27 AM
#6
The result will be:
j=10
i=5
....because b::i is initialized before b::j
-
June 27th, 2002, 11:09 AM
#7
Well, I tested the code on 2 different compilers,
GNU g++ and pgCC (Portland group).
Both gave the same answer, and the same answer that
kjetilh gave.
To be honest, I can not figure out why. Stroustrup says
that the variables will be initialized in the same order
as their definition, and makes a point of saying that it not being in
the same order as their declaration.
And when the definitions are changed to :
Code:
int B::j = i + 5;
int B::i = j + 5;
the results are consistent with Stroustrup's book.
-
June 27th, 2002, 01:41 PM
#8
yet, this is a bug or undefined result in VC++.
the i refered by
is clearly the i from the global scope:
to obtain 8 as a result for B::j one has to replace i declaration with
Code:
int B::j = ::i + 5;
Now why is that? beats me.
Martin Breton
3D vision software developer and system integrator.
-
June 27th, 2002, 04:33 PM
#9
Originally posted by kjetilh
The result will be:
j=10
i=5
....because b::i is initialized before b::j
Explain! 
How about this:
Code:
#include <iostream>
using namespace std;
class B
{
public:
static int i;
static int j;
};
int i, B::j = i + 5, B::i = 5;
int main()
{
cout << "j == " << B::j << endl
<< "i == " << B::i << endl;
return 0;
}
Oooooppppssss! You got the same result!
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
June 27th, 2002, 04:40 PM
#10
Originally posted by proxima centaur
yet, this is a bug or undefined result in VC++.
the i refered by
is clearly the i from the global scope:
to obtain 8 as a result for B::j one has to replace i declaration with
Code:
int B::j = ::i + 5;
Now why is that? beats me.
I use VC++ and I got the same results as Philip Nicoletti.
“An initializer for a static member is in the scope of the member’s class.” (TC++PL, 2nd edition). That is:
Code:
int B::j = i + 5; // equ int B::j = B::i + 5;
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
June 28th, 2002, 03:38 AM
#11
That's what I was thinking of when I suggested "something like Koenig lookup". In defence of the rule, it's like the way a member function definition bring things into scope in the .cpp file:
Code:
class foo
{
public:
class innerclass;
//.....
void bar(innerclass&);
};
void foo::bar(innerclass& ic)
{....
In the definition, you don't have to qualify "innerclass", since it's brought into scope by "foo::bar".
With the example given, B::i has "tighter" scope than ::i, so is used in prefence. The only issue that I couldn't look up is whether B::i is initialised at that point (declaration order) or not (definition order). It just emphasises the point "define/initialise member variables in the same order that you declare them"
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 28th, 2002, 09:50 AM
#12
The question is a stupid one!
This is a stupid question.
Why would any one write a piece of code like this that could easily trick the majority of programmers ( and could trick the compiler just as easy, too). As shown in previous follow ups, most people gets it wrong.
As a member of a software development team, your responsibility is NOT to write a piece of "smart" code that only you can understand correct and that is deemed to be mis-interpreted by other programmers, and even mis-compilered by the compiler, too! If you do so it doesn't show you as smart, it shows you as stupid.
Your responsibility, is to write crystal clear code which leaves absolutely no room for ambiguity what you intend to do and what happens first and what happens next. It needs to be so readable that even a junior programmer can read and understand it correctly. This is how a software team can produce bug free software.
-
June 28th, 2002, 09:54 AM
#13
Anthony,
That's not the point of the post. It's a piece of trivia, a point of interest, a starting point for discussion.
This was obviously not production code. Why attack it?
Jeff
-
June 28th, 2002, 10:27 AM
#14
Anthony,
You are mean… and I suspect you love it!
I do not think you are so stupid not too see the point of this discussion. Relax! This is not an arena!
I am waiting for your explanations. So far I do not have any. This weekend I will give a look at Mr. Stroustrup’s book and maybe I’ll find the answer.
I am not good at English, so please be brief.
Thanks Jeff!
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
June 28th, 2002, 12:54 PM
#15
Even though it is not "good" coding practice, it is still relevant to some of us who like to look at the arcane side of things and understand the twitches of such and such obscure code.
Personally, even though I wouldn't in my right mind write a piece of code like this one, I find it interesting to understand how the compiler works. This can actually prevent me for doing stupid things in my code in the future.
Now, unless you wrongfully felt your pride diminished because you didn't get the answer right or because you feel you are far superior to the lesser of us because you know what "good" code is, please stop telling others how stupid they are, as I myself find your comment useless as it brings nothing as to why the compiler acts the way it does.
By the way, I totally agree with the rest of your comment.
Martin Breton
3D vision software developer and system integrator.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|