Static data members initialization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Visame
    New Member
    • Dec 2007
    • 7

    #1

    Static data members initialization

    The following example is from C++ Standard(ISO148 82)
    9.4.2 Static data members
    class process {
    static process* run_chain;
    static process* running;
    };
    process* process::runnin g = get_main();//what does get_main() mean here?
    process* process::run_ch ain = 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<<end l;
    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)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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::Avaria ble.

    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.

    Comment

    Working...