RE: C++ static integer class constants...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] On
Behalf Of John Ratliff
Sent: Sunday, October 16, 2005 3:36 AM
To: gcc-help@xxxxxxxxxxx
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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux