static memeber initialization in constructor??

lrtaylor@micron.com lrtaylor@micron.com
Mon Nov 1 20:25:00 GMT 2004


You actually have to also define 'one' someone in your implementation
code just like you do your functions:

long A::one[MAX];

That actually causes space to be allocated for 'one'.  The declaration
just declares it (kind of like when you use 'extern' - you have to have
it actually defined someone).

Thanks,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Ishwar Rattan
Sent: Monday, November 01, 2004 1:25 PM
To: gcc-help@gcc.gnu.org
Subject: static memeber initialization in constructor??

g++ 3.2.2 under Linux Mandrake-9.1

I have a class A being derived from base class B

class A: public B
{
   public:

      A() : B("starter") { init_one(); }

   // other public methods..

   protected:
      const int MAX = 10;
      static long one[MAX];
      static void init_one()
      {
         for(int i = 0; i < MAX; i++)
	    one[i] = 9;
      }
};


Whem compiled the ld fails with message that there is undefined
reference to
 A::one

So, can one initialize a static member in the above scenario?

-ishwar



More information about the Gcc-help mailing list