473,387 Members | 1,440 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,387 software developers and data experts.

Static data members initialization

7
The following example is from C++ Standard(ISO14882)
9.4.2 Static data members
class process {
static process* run_chain;
static process* running;
};
process* process::running = get_main();//what does get_main() mean here?
process* process::run_chain = running;

C++ Standard also says in:
5.19 Constant expressions
In several places, C + + requires expressions that evaluate to an
integral or enumeration constant: as array bounds , as case
expressions , as bit-field lengths , as enumerator initializers ,as
static member initializers, and as integral or enumeration non-type
template arguments.

get_main() is not constant expression,is it? How can it be a "static
member initializer"?

WHEN is static data member is initialized?
The following two code snippets puzzle a lot of people.
1 #include <iostream>
2 using namespace std;
3 class a{
4 public:
5
6 static int i;//declaration
7 };
8 int f(){ return a::i+7;}
9 int a::i = f();//definition herer
10
11 int main()
12 {
13 a Aa;
14 cout << Aa.i;
15 }
//The output is 7 on gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
************************************************** **********************************************
1 #include <iostream>
2 using namespace std;
3 class a{
4 public:
5
6 static int i;
7 };
8 void f()
9 {
10 cout<<a::i<<endl;
11 }
12 int a::i = 9;
13
14 int main()
15 {
16 f();
17 }
//The output is 9 on gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
May 3 '08 #1
1 2390
weaknessforcats
9,208 Expert Mod 8TB
static class members are not part of any particular object. They are shared by all objects.

Therefore, you cannot initialize a static class member using a constructor.

Therefore, the static class member must be initialized before any object is created. However, there is one exception here: If the static member is const, then you can initialize it in the class definition. The effect is to create a const varibable in every source file that includes the class header. This is very ugly and I wouldn't do myself, but it is legal.

Therefore, you have to define (not declare) the static member as a global variable. Be careful not to make this global variable static. If you don't understand why, then read up on linkage.

Therefore, the static class member can be used without an object. You just refer to it using the full name: MyClass::Avariable.

Therefore, static class member functions have no this pointer.

Therefore, startic class member functions cannot be put in a VTBL.

Therefore, you cannot have a virtual static class member function.

Therefore, you cannot use a static class member function with polymorphism.

Therefore, you cannot use static class member functions in object-oriented programming.

Other than the above, they're great. Just avoid them like the plague.
May 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
1
by: Eric Lilja | last post by:
Hello, I have a class in my program and there may be several objects around of the class at any given time. The class has seven data members that qualify for static linkage, but only one of them...
3
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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
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
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...

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.