Topic: Q: Why static data initialised in .cpp file?
Author: Marcelo Cantos <marcelo@janus.mds.rmit.edu.au>
Date: 1997/08/25 Raw View
"Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net> writes:
> The way to initialise static data is in the .cpp file:
>
> class X
> {
> public:
> static int a;
> };
>
> 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.
>
> 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?
Bad move. If X::a is a constant it is safe to do this. The other
alternative for constants is to use enum (I actually prefer this,
since it has wider compiler support).
If it is a variable, however, it should be initialised in a .c file,
otherwise it may appear in multiple compilation units and hence
generate link errors.
--
______________________________________________________________________
Marcelo Cantos, Research Assistant __/_ marcelo@mds.rmit.edu.au
Multimedia Database Systems Group, RMIT / _ Tel 61-3-9282-2497
L2/723 Swanston St, Carlton VIC 3053, Aus/ralia ><_>Fax 61-3-9282-2490
Acknowledgements: errors - me; wisdom - God; sponsorship - RMIT
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jensk@bbn.hp.com (Jens Kilian)
Date: 1997/08/25 Raw View
Cristian Georgescu (Cristian.Georgescu@worldnet.att.net) wrote:
> 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.
AFAIK, this is explicitly allowed by the draft standard.
> 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?
In current C++ compilers, you can use enums:
class FOO {
public:
enum { BAR = 1, BAZ = 42, GAZONK = 4711 };
};
This works as long as all your constants are distinct integers; if they aren't
distinct, just use multiple enums. If they aren't integers, this won't work.
HTH,
Jens.
PS: Your ISP just sent me some UCE today. You may consider switching it.
--
mailto:jjk@acm.org phone:+49-7031-14-7698 (HP TELNET 778-7698)
http://www.bawue.de/~jjk/ fax:+49-7031-14-7351
PGP: 06 04 1C 35 7B DC 1F 26 As the air to a bird, or the sea to a fish,
0x555DA8B5 BB A2 F0 66 77 75 E1 08 so is contempt to the contemptible. [Blake]
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Colin Rafferty <craffert@ml.com>
Date: 1997/08/26 Raw View
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 ]
Author: "Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net>
Date: 1997/08/25 Raw View
The way to initialise static data is in the .cpp file:
class X
{
public:
static int a;
};
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.
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?
--
Cristian Georgescu
_________________________________________________
Smith Industries
Aerospace & Defense Systems
7-9 Vreeland Road,
Florham Park, NJ 07932, USA.
_________________________________________________
E-mail: Georgescu_Christian@si.com
---
[ 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 ]