Thread: Static Initilization question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Static Initilization question

    Hi all,
    Suppose i have a class A, a static instance of which is used in a class B.
    All i need is to use the nonstatic void A::initilize() function to initilize the static object A.
    I tried something like the following
    Code:
    A B::instace_of_A.initilize();
    but it didn't work.
    May someone tell me how i shall do this?
    Thanks..

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to call that in another function. If it can be called more than once, you can call it in your B constructor, so every time a B instance is created it is called. This only makes sense if it is basically a no-op when it is called after the first time. You can write a separate function (or static member function of B) and call this once at the start of your program (or somewhere else). You can also create another class that calls that function in the constructor, then make a static member of that class inside B:
    Code:
    class B
    {
      static A instance_of_A;
      class A_Initializer
      {
      public:
        A_Initializer() { B::instance_of_A.initialize(); }
      };
      static A_Initializer initializer;
    };
    
    // in cpp file:
    A B::instance_of_A;
    B::A_Initializer B::initializer;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Static lib question
    By X PaYnE X in forum Windows Programming
    Replies: 2
    Last Post: 08-08-2006, 11:55 AM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. question about static members
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2003, 10:21 AM