471,585 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

some question about static member

Hi, all!
I need some experieced coder to explain what the following code will
produce, thank you!

#inlcude <iostream>
using namespace std;

class WithStatic{
static int x;
static int y;
public:
void print(void);
};
void WithStatic::print(void)
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
int x = 100;
int WithStatic::y = x + 1;
int WithStatic::x = 10;

int main()
{
WithStatic ws;
ws.print();
}
Jul 22 '05 #1
2 1051

"Qin Chen" <wi*******@etang.com> wrote in message
news:c1*************@ID-226321.news.uni-berlin.de...
Hi, all!
I need some experieced coder to explain what the following code will
produce, thank you!
It produces a compiler error. Either you didn't try it yourself, or you
didn't cut and paste your actual code here. Is this a homework assignment?
Why do you need an instance of WithStatic?
#inlcude <iostream>
using namespace std;

class WithStatic{
static int x;
static int y;
public:
void print(void);
};
void WithStatic::print(void)
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
int x = 100;
int WithStatic::y = x + 1;
int WithStatic::x = 10;

int main()
{
WithStatic ws;
ws.print();
}

Jul 22 '05 #2
On Wed, 25 Feb 2004 22:53:06 +0800, "Qin Chen" <wi*******@etang.com>
wrote:
Hi, all!
I need some experieced coder to explain what the following code will
produce, thank you!

#inlcude <iostream>
#include <iostream>
using namespace std;

class WithStatic{
static int x;
static int y;
public:
void print(void);
};
void WithStatic::print(void)
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
int x = 100;
int WithStatic::y = x + 1;
int WithStatic::x = 10;

int main()
{
WithStatic ws;
ws.print();
}


That code must produce:

x = 10
y = 11

This is because the initializer for a static variable is in the scope
of the class. Members of the class can be used without qualification,
and hide names from enclosing scopes to the initialization, so the "x
+ 1" refers to "WithStatic::x + 1".

Because WithStatic::x is statically initialized but WithStatic::y is
dynamically initialized (with the non-constant expression "x + 1"),
the fact that it appears before the x initialization doesn't matter -
it will still get the correct value of WithStatic::x. This is similar
to:

extern int i;
int j = i + 1; //dynamic initialization happens later
int i = 10; //static initialization happens first

Here j == 11.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by van | last post: by
3 posts views Thread by Tran Tuan Anh | last post: by
7 posts views Thread by Tony Johansson | last post: by
8 posts views Thread by Yong W | last post: by
39 posts views Thread by Digital Puer | last post: by
2 posts views Thread by .rhavin grobert | last post: by
13 posts views Thread by yanlinlin82 | last post: by
19 posts views Thread by Marco Trapanese | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.