473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1104

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
2
by: van | last post by:
Writing classes of some algorithms, some parameters needs to be passed into the classes, the actual algorithms are contained in member functions of classes. What I am thinking is having some...
3
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
8
by: Yong W | last post by:
if A and B are Class type, what's mean about the next sentence? " int A::*B::*pMember; " And how to use this pointer ? we assume A and B's definition is: class A { int a;
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
13
by: yanlinlin82 | last post by:
I'd like to write all code of a class in only header file. But when there is any static member varia ble, I have to define it in cpp file, not the header file. Otherwise, the static member variable...
19
by: Marco Trapanese | last post by:
Hello, I need some help to understand what I'm doing :) I'm writing code for an embedded system and I need to create a navigable menu system. I'm going to use these structures: typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.