473,406 Members | 2,705 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,406 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 1105

"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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.