Order of initialization of static variables

This is a discussion on Order of initialization of static variables within the C++ Programming forums, part of the General Programming Boards category; Consider the following code: Code: int f(); struct A { static int a; } int A::a = f(); int f() ...

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Order of initialization of static variables

    Consider the following code:
    Code:
     
    int f();
     
    struct A
    {
      static int a;
    }
    int A::a = f();
     
    int f()
    {
      static int i = 0;
      return i;
    }
    What will A::a be equal to?
    Couldn't A::a be initialized before f::i?

    The reason I ask is the thread "Virtual Constructors" where my solution depends on the static variables being initialized in a particular order.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The rules are a bit obscure, but both A::a and f::i are zero-initialized before anything else. Then A::a is dynamically initialized with the value of f(), which sends execution through f to initialize f::i to the constant expression you give it and returns that value. So A::a will have the same value as f::i. A better example would give f::i a non-default value such as 10.
    My best code is written with the delete key.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I,m really curious Prelude, where and when you learned that. I have a few books about C++ and never read it anywhere. Do you learn from books + pratice or just write programs?
    I must admit that I never thought about some things such as this, if you could give some advices to how to learn it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I,m really curious Prelude, where and when you learned that.
    I have a copy of the C++ standard.

    >I have a few books about C++ and never read it anywhere.
    What you can learn depends on the books. If they're beginner books then topics of an advanced nature won't be mentioned. For extremely detailed questions about the language, the standard is as good as it gets, but it reads like VCR instructions.

    >Do you learn from books + pratice or just write programs?
    Well, my library is somewhere around 200 books right now, so I would have to say that I learn by reading and doing, not to mention answering questions here.

    >if you could give some advices to how to learn it.
    The best way is to read everything you can and apply it. A way to apply what you know as well as get feedback on it is to answer questions here. You'll learn much more quickly if you try to help others to understand something. First, it helps you to solidify your understanding and put it into words. Second, it gives everyone else a chance to double check you and correct you if you're wrong. Third, any test or example code you write will add to your experience. That three-fold method of learning is surprisingly effective.
    My best code is written with the delete key.

  5. #5
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    >Well, my library is somewhere around 200 books right now.
    Jesus and I though 15 was too many .But I second that on posting here,It has helped me alot.
    big146

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21