Topic: Why static data initialised in .cpp file?


Author: "Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net>
Date: 1997/08/26
Raw View

     In the text of my first question, I have made a couple of mistakes
     that have obscured the meaning. I have also not very well explained
     what I wanted. Let me try again; this time I will take a slightly more

     complicated example.

     I have a class X that has a class attribute static const int address;
     The class X also has a class attribute static const string key;

     The static const have to be defined and initialised in the .cpp file.

     I write here the code for the .h and then for the .cpp file:

---- In the X.h file:

         enum { ADR1 = 0xF1, ADR2 = 0xF2, ADR3 = 0xF3 };
         const string KEY1 = "first";
         const string KEY2 = "second";
         const string KEY2 = "third";


         class X
         {
           public:
                 static const int address;
                 static const string key;
         };

---- And in the X.cpp file:

         int X::address = ADR1;
         int X::key     = KEY1;

I would like that the users are aware of the values of X::address and
X::key.
Why? Because I want that they are able to make use of the fact that
X::address=ADR1, for instance.

> It is a very idea to make the users of class X aware of the value of
X::a, and
> make them hardcode it into their software.  If you later change its value
to
> 0xFFFF, you will force the users to change their code (rather than
recompile).

Of course that this will imply that if I will decide to change the int
X::address = ADR1; to lets say: int X::address = ADR2; then this will
require a
recompilation. However in the specific application I have in mind, these
attributes are not supposed to change.

The whole idea is that the users have to write code and use directly the
value
of the static const. Therefore they are supposed to KNOW the value of this
variable, and this is why I argue that this initialisation should appear in
the
h file because this is the interface for the users.

> If you want X::a to be a compile-time constant, you can make it an enum
> instead of a variable:
> .....................

It is true that I can use an enum for the address, as you have suggested:

         class X
         {
           public:
                 enum { address = ADR1 };
                 static const string key;
         };

But how should I handle the "key" which is a string, not an int (and
therefore
canot be converted to an enum)?

Besides, I do not want a compile time constant for address, because I want
to
make sure that I create a storage for this variable and I also want to put
this
const variable in ROM (Read Only Memory), and I also want to place the
variable
at a specific location in ROM.

>> Perhaps a better way to do it is to put the int X::a = 0xFF; in the .h
file
>> with a #ifdef macro directive? Or would this get me into trouble
somehow?

> It will only get you in trouble with the people who will later maintain
your
> code. :-)

I myself am not very happy with the solution of putting the initialisation
of
the static variable in the .h file either. However I do not quite see a
clear
solution for now.

Besides, I would be gratefull if you can point out the ways the trouble can

happen, maybe it can be avoided?
     _________________________________________________
     Cristian Georgescu

     Work         Smith Industries
     address:     Aerospace & Defense Systems
                  7-9 Vreeland Road,
                  Florham Park, NJ 07932, USA.

     E-mail:      Georgescu_Christian@si.com

     tel:         1 (201) 822-1300 / 2229
     _________________________________________________




______________________________ Reply Separator
_________________________________
Subject: Re: Q: Why static data initialised in .cpp file?
Author:  Colin Rafferty <craffert@ml.com> at SMTPpost
Date:    8/25/97 2:14 PM


The following message is a courtesy copy of an article
that has been posted to comp.std.c++ as well.

Cristian Georgescu writes:

> The way to initialise static data is in the .cpp file:
>         class X
>         {
>           public:
>                 static int a;
>         };

Actually, it is in the .h file, and should be `static const int a', but
I assume that was a typo.

> And in the X.cpp file:

>         int X::a = 0xFF;

> How ever I consider that the initialisation value for the static
attribute
> is part of the interface of the class and therefore it should appear in
the
> h file.

It is a very idea to make the users of class X aware of the value of
X::a, and make them hardcode it into their software.  If you later
change its value to 0xFFFF, you will force the users to change their
code (rather than recompile).

If you want X::a to be a compile-time constant, you can make it an enum
instead of a variable:

        class X
        {
          public:
                enum { a = 0xFF };
        };

However, the users of your class should still be using X::a, and never
0xFF.

> Perhaps a better way to do it is to put the int X::a = 0xFF; in the .h
file
> with a #ifdef macro directive? Or would this get me into trouble somehow?

It will only get you in trouble with the people who will later maintain
your code. :-)

--
Colin

---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]