static array initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Makis Papapanagiotou

    static array initialization

    Hello,

    I really don't understand the simplest thing at the moment. I have a static
    array of pointers to a class . How do we initialize it?

    class classA
    {
    public:
    int x1;
    };

    class classB
    {
    public:
    void foo() { cout<< m_ptr[0]->x1; }
    static classA* m_ptr[4];
    };

    classA* classB::m_ptr[0]=0; // This is not correct !!!
    classA* classB::m_ptr[1]=0; // This is not correct !!!
    classA* classB::m_ptr[2]=0; // This is not correct !!!
    classA* classB::m_ptr[3]=0; // This is not correct !!!

    int main()
    {
    classB* ptr = new classB;

    ptr->foo();

    return 0;
    }

    Any idea? If "m_ptr" was not an array of pointers I would had initialize it
    like this: "classA* classB::m_ptr=0 ;"
    What about arrays now?


  • John Harrison

    #2
    Re: static array initialization


    "Makis Papapanagiotou" <Serafim.Papapa nagiotou@siemen s.com> wrote in message
    news:bj409b$hkt $1@news.mch.sbs .de...[color=blue]
    > Hello,
    >
    > I really don't understand the simplest thing at the moment. I have a[/color]
    static[color=blue]
    > array of pointers to a class . How do we initialize it?
    >
    > class classA
    > {
    > public:
    > int x1;
    > };
    >
    > class classB
    > {
    > public:
    > void foo() { cout<< m_ptr[0]->x1; }
    > static classA* m_ptr[4];
    > };
    >
    > classA* classB::m_ptr[0]=0; // This is not correct !!!
    > classA* classB::m_ptr[1]=0; // This is not correct !!!
    > classA* classB::m_ptr[2]=0; // This is not correct !!!
    > classA* classB::m_ptr[3]=0; // This is not correct !!!
    >
    > int main()
    > {
    > classB* ptr = new classB;
    >
    > ptr->foo();
    >
    > return 0;
    > }
    >
    > Any idea? If "m_ptr" was not an array of pointers I would had initialize[/color]
    it[color=blue]
    > like this: "classA* classB::m_ptr=0 ;"
    > What about arrays now?
    >[/color]

    Same way you initialise any array

    classA* classB::m_ptr[4] = { 0, 0, 0, 0 };

    but since zero initialisation is the default, you don't need to initialise
    it at all.

    classA* classB::m_ptr[4];

    john


    Comment

    • Josephine Schafer

      #3
      Re: static array initialization


      "Makis Papapanagiotou" <Serafim.Papapa nagiotou@siemen s.com> wrote in message
      news:bj409b$hkt $1@news.mch.sbs .de...[color=blue]
      > Hello,
      >
      > I really don't understand the simplest thing at the moment. I have a[/color]
      static[color=blue]
      > array of pointers to a class . How do we initialize it?
      >
      > class classA
      > {
      > public:
      > int x1;
      > };
      >
      > class classB
      > {
      > public:
      > void foo() { cout<< m_ptr[0]->x1; }
      > static classA* m_ptr[4];
      > };
      >
      > classA* classB::m_ptr[0]=0; // This is not correct !!!
      > classA* classB::m_ptr[1]=0; // This is not correct !!!
      > classA* classB::m_ptr[2]=0; // This is not correct !!!
      > classA* classB::m_ptr[3]=0; // This is not correct !!![/color]

      Try this -
      classA* classB::m_ptr[]={0};





      Comment

      Working...