public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* static memeber initialization in constructor??
@ 2004-11-01 20:19 Ishwar Rattan
  2004-11-01 20:33 ` Paweł Sikora
  0 siblings, 1 reply; 3+ messages in thread
From: Ishwar Rattan @ 2004-11-01 20:19 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: static memeber initialization in constructor??
  2004-11-01 20:19 static memeber initialization in constructor?? Ishwar Rattan
@ 2004-11-01 20:33 ` Paweł Sikora
  0 siblings, 0 replies; 3+ messages in thread
From: Paweł Sikora @ 2004-11-01 20:33 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 766 bytes --]

On Monday 01 of November 2004 21:24, Ishwar Rattan wrote:
> 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?

You need minor fix.

-- 
/* Copyright (C) 2003, SCO, Inc. This is valuable Intellectual Property. */

                           #define say(x) lie(x)

[-- Attachment #2: 0.diff --]
[-- Type: text/x-diff, Size: 362 bytes --]

--- 0.cpp.orig	2004-11-01 21:28:48.342960088 +0100
+++ 0.cpp	2004-11-01 21:28:53.028247816 +0100
@@ -7,7 +7,7 @@
    // other public methods..
 
    protected:
-      const int MAX = 10;
+      const static int MAX = 10;
       static long one[MAX];
       static void init_one()
       {
@@ -15,3 +15,5 @@
 	    one[i] = 9;
       }
 };
+
+long A::one[A::MAX];

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: static memeber initialization in constructor??
@ 2004-11-01 20:25 lrtaylor
  0 siblings, 0 replies; 3+ messages in thread
From: lrtaylor @ 2004-11-01 20:25 UTC (permalink / raw)
  To: ishwar, gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-11-01 20:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-01 20:19 static memeber initialization in constructor?? Ishwar Rattan
2004-11-01 20:33 ` Paweł Sikora
2004-11-01 20:25 lrtaylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).