This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: C++ static integer class constants...
- From: Ryan Mansfield <RMansfield at qnx dot com>
- To: 'John Ratliff' <webmaster at technoplaza dot net>, gcc-help at gcc dot gnu dot org
- Date: Sun, 16 Oct 2005 11:26:26 -0400
- Subject: RE: C++ static integer class constants...
Yes, it is required that static data members must be defined in exactly one
translation unit.
Please see:
http://gcc.gnu.org/onlinedocs/gcc/Static-Definitions.html#Static-Definitions
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11
Regards,
Ryan Mansfield
-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of John Ratliff
Sent: Sunday, October 16, 2005 3:36 AM
To: gcc-help@gcc.gnu.org
Subject: C++ static integer class constants...
A few days ago, I was writing a program that had some constants. They were
all defined in a class and were static constant integers, something like
class foo {
public:
static const int X = 5;
static const int Y = 10;
static const int Z = 15;
};
When I would compile my program in Windows XP with mingw g++ 3.4.2, I would
have no problems.
However, when I went to Linux, where I have g++ 3.3.5 (or possibly 3.3.6,
but I think it's 3.3.5), the linker would complain about unresolved symbols.
If I were to define the static variable in my implementation file, the
linker would find the variables and go on its merry way. In other words, I
could solve the problem by doing this:
const int foo::X;
const int foo::Y;
const int foo::Z;
I didn't even need to declare a value here. It took the value from the
class-def.
Is this correct behavior? I thought static constant integers could be
defined completely within the class, because they are simply type-safe
constants that need no storage space. It seems like it might have been wrong
since in Windows I had g++ 3.4.2 and in Linux I had g++ 3.3.5, but I wasn't
sure.
In Bruce Eckel's "Thinking in C++", he states that "In older versions of
C++, static const was not supported inside classes." I don't generally think
of g++ 3.3.x as an old compiler, but maybe it is just old enough not to
support this?
Thanks,
--John Ratliff