public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: static memeber initialization in constructor??
@ 2004-11-01 20:25 lrtaylor
  2004-11-19  0:04 ` Question on derived class? Ishwar Rattan
  0 siblings, 1 reply; 5+ 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] 5+ messages in thread

* Question on derived class?
  2004-11-01 20:25 static memeber initialization in constructor?? lrtaylor
@ 2004-11-19  0:04 ` Ishwar Rattan
  2004-11-19  0:20   ` Eljay Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Ishwar Rattan @ 2004-11-19  0:04 UTC (permalink / raw)
  To: gcc-help


Assume that A is pure virtual class with some public
methods.

B is derived from A

Class B : virtual A {
public:
     // all methods of A are defined here +
     A new method is also defined (here only)
     -- call it reset()
protected:
     // ...
};

Now B is initiated as

static B b;
A *a = b;

Is it possible to access b.reset() using a->reset()?

-ishwar

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

* Re: Question on derived class?
  2004-11-19  0:04 ` Question on derived class? Ishwar Rattan
@ 2004-11-19  0:20   ` Eljay Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: Eljay Love-Jensen @ 2004-11-19  0:20 UTC (permalink / raw)
  To: Ishwar Rattan, gcc-help

Hi Ishwar,

Your question is a general C++ question, and off-topic for this forum.

 >Is it possible to access b.reset() using a->reset()?

Yes.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ 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; 5+ 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] 5+ messages in thread

* static memeber initialization in constructor??
@ 2004-11-01 20:19 Ishwar Rattan
  2004-11-01 20:33 ` Paweł Sikora
  0 siblings, 1 reply; 5+ 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] 5+ messages in thread

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-01 20:25 static memeber initialization in constructor?? lrtaylor
2004-11-19  0:04 ` Question on derived class? Ishwar Rattan
2004-11-19  0:20   ` Eljay Love-Jensen
  -- strict thread matches above, loose matches on Subject: below --
2004-11-01 20:19 static memeber initialization in constructor?? Ishwar Rattan
2004-11-01 20:33 ` Paweł Sikora

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).