This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
C++ static integer class constants...
- From: "John Ratliff" <webmaster at technoplaza dot net>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Sun, 16 Oct 2005 02:35:52 -0500
- 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