public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
[parent not found: <0IOJ0024ULK8BF@emroute1.cind.ornl.gov>]
[parent not found: <87oe5pb5dp.fsf@totally-fudged-out-message-id>]
[parent not found: <200510160743.j9G7hVoa010304@inbound-smtp-1.corp.adobe.com>]
[parent not found: <20051016200125.C061F13137C@arvo.suso.org>]
* RE: C++ static integer class constants...
@ 2005-10-16 15:26 Ryan Mansfield
  2005-10-16 16:16 ` John Ratliff
                   ` (4 more replies)
  0 siblings, 5 replies; 29+ messages in thread
From: Ryan Mansfield @ 2005-10-16 15:26 UTC (permalink / raw)
  To: 'John Ratliff', gcc-help

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


^ permalink raw reply	[flat|nested] 29+ messages in thread
[parent not found: <435202e6.3d759d8f.4550.fffff1e6SMTPIN_ADDED@mx.gmail.com>]
* C++ static integer class constants...
@ 2005-10-16  7:35 John Ratliff
  0 siblings, 0 replies; 29+ messages in thread
From: John Ratliff @ 2005-10-16  7:35 UTC (permalink / raw)
  To: gcc-help

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



^ permalink raw reply	[flat|nested] 29+ messages in thread
* C++ static integer class constants...
@ 2005-10-16  7:35 John Ratliff
  0 siblings, 0 replies; 29+ messages in thread
From: John Ratliff @ 2005-10-16  7:35 UTC (permalink / raw)
  To: gcc-help

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



^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2005-10-18 12:26 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20051016195314.7EFB824AEF5@lax-gw02.mroute.net>
2005-10-17  9:09 ` C++ static integer class constants Nathan Sidwell
2005-10-17 19:21   ` Florian Weimer
     [not found] <0IOJ0024ULK8BF@emroute1.cind.ornl.gov>
2005-10-18 12:26 ` Ernest L. Williams Jr.
     [not found] <87oe5pb5dp.fsf@totally-fudged-out-message-id>
2005-10-17 19:17 ` Florian Weimer
2005-10-18  6:19   ` John Ratliff
2005-10-18  6:19   ` John Ratliff
     [not found] <200510160743.j9G7hVoa010304@inbound-smtp-1.corp.adobe.com>
2005-10-17 12:05 ` John Love-Jensen
2005-10-17 17:50   ` John Ratliff
2005-10-17 17:50   ` John Ratliff
     [not found]   ` <4353e481.0829b6d6.64bd.18a4SMTPIN_ADDED@mx.gmail.com>
2005-10-17 18:07     ` corey taylor
2005-10-17 20:04       ` John Ratliff
2005-10-17 20:04       ` John Ratliff
     [not found] <20051016200125.C061F13137C@arvo.suso.org>
2005-10-16 21:36 ` John Ratliff
2005-10-16 21:36 ` John Ratliff
2005-10-16 15:26 Ryan Mansfield
2005-10-16 16:16 ` John Ratliff
2005-10-16 16:16 ` John Ratliff
2005-10-16 16:42 ` John Ratliff
2005-10-16 16:42 ` John Ratliff
     [not found] ` <43528310.03f34613.6a05.0e40SMTPIN_ADDED@mx.gmail.com>
2005-10-16 17:43   ` Alex J. Dam
     [not found]     ` <4352a3b2.42389977.3b5e.60fdSMTPIN_ADDED@mx.gmail.com>
2005-10-16 19:29       ` Alex J. Dam
2005-10-16 19:53         ` John Ratliff
2005-10-16 19:53         ` John Ratliff
2005-10-16 19:45     ` John Ratliff
2005-10-16 19:45     ` John Ratliff
     [not found] <435202e6.3d759d8f.4550.fffff1e6SMTPIN_ADDED@mx.gmail.com>
2005-10-16 14:50 ` Alex J. Dam
2005-10-17 19:13   ` Florian Weimer
  -- strict thread matches above, loose matches on Subject: below --
2005-10-16  7:35 John Ratliff
2005-10-16  7:35 John Ratliff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).