Thread: Need for a Static Constructor?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Need for a Static Constructor?

    I know we cannot use static as modifier for a constructor

    but we do need a static constructor to initialize all the static data for a class.

    C# explicitly provides static constructors for this. Even Java has unnamed static blocks that emulate such action

    Why has no such facility been provided in C++?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    static members are initialized where they are defined. I'm guessing the problem is that you would like to have a block of code so that you can do other work before initializing the variables. My guess is that it is not included at least in part because you can simulate the same thing by wrapping the variable(s) in a class and making the class the static member. Then the initialization can be done in that class's constructor.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Static data is accessible even though no object has been instantiated from that class. Having a static constructor would leave all those objects initialized until the first instantiation. Static "data" has to be initialized in an implementation with the following syntax:

    variable-type class-name::variable-name = value;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yeah, but the problem is that code is not in function scope, so you can't do other stuff before or after it. A static constructor would really just wrap a function around that while still ensuring that the code is run at the start of the program.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But C++ has no well-defined order of dynamic initialization anyway, which makes the whole thing somewhat problematic. There is no class loader, which detects exactly when a class is first used. Modules in C++ are linked together at compile time, without any dependency analysis. The relative order of dynamic initialization of objects in different modules is unspecified. This means that any dynamic initialization code of global objects that accesses dynamically initialized objects in other modules potentially finds these objects in an uninitialized and therefore undefined state.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    C++ doesn't necessarily need a static constructor - as always, there are alternatives which fit the bill -

    http://www.parashift.com/c++-faq-lit...html#faq-10.12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM