how to initialize a static pointer array in a class?

This is a discussion on how to initialize a static pointer array in a class? within the C++ Programming forums, part of the General Programming Boards category; Hello, I don't know how to initialize a static pointer array in a class. I wish to write something like ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    17

    how to initialize a static pointer array in a class?

    Hello,

    I don't know how to initialize a static pointer array in a class.

    I wish to write something like the following code only with a class:

    Code:
    struct Test{
       int xCount;
    };
    
    int main(){
    
       Test * myCounter[10];
       
       myCounter[0] = new Test;
       (*myCounter[0]).xCount = 0;
    
    }

    Here is the last thing I have tried... but it doesn't compile:

    Code:
    //myClass.h
       class myClass{
          protected:
             static struct Test { int xCount; } * myCounter[10];
       };
    
    //myClass.cpp
       #include "myClass.h"
       myClass::Test myClass::*myCounter[0] = new myClass::Test;
    Obviously I have left out lots of stuff (i.e. constructor) from the class code here but I am only having problems getting this thing initialized and getting the correct code written for "(*myCounter[0]).xCount = 0;" in my class.

    Any help or suggestions would be greatly appreciated!
    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You cannot initialize the values of the elements in the array globally. That code must be in a function. First you need to define the array (almost like you have it, but without the assignment part) because it is a static member of the class. Then you need to initialize it in a static method of the class that calls new to create each object. The syntax for that is similar to your first example.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    Thanks for the help!

    I did what you said:
    Code:
       myClass::Test myClass::*myCounter[10];
    But the compiler errored and said struct Test was protected. I made the only struct definition public and kept the static protected... and it worked!

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    Now that my define is working... I can't get it to initialize.

    I trying to initialize it in my constructor:

    myCounter[0] = new myClass::Test;
    (*myCounter[0]).xCount = 0;

    I am getting errors on both lines. Does anyone have the syntax for this?

    Thanks!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    myClass::Test myClass::*myCounter[10];
    should be
    Code:
    myClass::Test *myClass::myCounter[10];
    That was causing the problem with protected, and is probably causing your errors now.

    BTW, you should not initialize the static array in a constructor for the class, because the constructor for the class is called for every instance, and you would be re-initializing every time a new instance of the class is created.

    You can create a static method that initializes it, then call the static method at the beginning of your program. You can also keep a static bool variable that remembers whether it has been initialized or not. Whenever you want to use it you check that variable and if it is false you call the initialization function (which also sets the variable to true so it is only initialized once). Either way you want to keep the initialization of static objects in a place that will only be called once.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    That was it! Thanks for the info!

    Yep, I already had a bool set... just testing in the constructor until I had the syntax worked out.

    Thanks again!

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You cannot initialize the values of the elements in the array globally. That code must be in a function. First you need to define the array (almost like you have it, but without the assignment part) because it is a static member of the class. Then you need to initialize it in a static method of the class that calls new to create each object. The syntax for that is similar to your first example.
    In some cases you can have a static object(which is an object outside of any function) call the initialization method in its constructor. (I leave the a initialization method private and have the static object's class be a friend's of the class with static members.) This technique works like global initialization because the static method gets called when the static object's constructor gets called.
    Last edited by okinrus; 09-28-2005 at 10:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 02:14 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. using static on a pointer
    By richdb in forum C Programming
    Replies: 4
    Last Post: 04-17-2006, 05:16 AM
  5. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 01:10 PM

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