Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10110
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Ian Collins <ian-news@hotmail.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: static constants in a class |
| Date | Thu, 15 Sep 2011 10:21:47 +1200 |
| Lines | 80 |
| Message-ID | <9dcnnrF784U8@mid.individual.net> (permalink) |
| References | <ygfboumyf3c.fsf@janus.isnogud.escape.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net 7107E21jQK/0FkutO99Ltg/k5IYLQTZE3RpHbofwxrWbwBTuT1 |
| Cancel-Lock | sha1:UvVRju7rSTzlnhgQoq5aITdAKvI= |
| User-Agent | Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 |
| In-Reply-To | <ygfboumyf3c.fsf@janus.isnogud.escape.de> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c++:10110 |
Show key headers only | View raw
On 09/15/11 09:43 AM, Urs Thuermann wrote:
> How should one define a constant, say of type int, in a class? The
> only way that worked for me in all cases is to delcare the constant in
> the class in the header file and define its value in the
> implementation file:
>
> ---- foo.hh ----
> class Foo {
> public:
> static const int N;
> ...
> };
> ---- foo.cc ----
> #include "foo.hh"
> const int Foo::N = 10;
> ----------------
>
> But this will prevent many optimizations by the compiler, e.g. when I
> compile
>
> ---- bar.cc ----
> void f() {
> int array[N];
This shouldn't compile, C++ doesn't have VLAs (yet, and they are
optional in the new standard).
> for (int i = 0; i< N; i++) { ... }
This isn't really an optimisation issue, it is unlikely the compiler
would use an immediate compare in a loop construct.
> since the compiler doesn't know the value of N. Is it possible to
> have the constant in foo.hh, i.e. visible in all translation units
> including foo.hh, without getting other problems?
>
> I tried two other ways, both having problems:
>
> 1. class Foo {
> public:
> static const int N;
> ...
> };
> const int Foo::N = 10;
>
> If I include this into multiple translation units I get multiple
> definitions of N and therefore a linker error.
Correct.
> 2. class Foo {
> public:
> static const int N = 10;
> ...
> };
>
> This seemed to work at first glance, since the compiler will
> optimize most accesses to N away and use the value directly.
>
> But when N is used where a reference is needed, e.g.
It will work as long as the address of N isn't required.
> some_std_list.push_back(N);
>
> my compiler (gcc) produces an access to N which however is not
> defined anywhere so I get a linker error again.
Correct.
> The traditional C way would be to use #define, but I wanted to avoid
> that. Is there way to have the constant definition in the header
> file?
Use the cleaner C way: an enum constant.
enum{ N = 10 };
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
static constants in a class Urs Thuermann <urs@isnogud.escape.de> - 2011-09-14 23:43 +0200
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-15 10:21 +1200
Re: static constants in a class Leigh Johnston <leigh@i42.co.uk> - 2011-09-15 00:06 +0100
Re: static constants in a class Leigh Johnston <leigh@i42.co.uk> - 2011-09-15 00:16 +0100
Re: static constants in a class Juha Nieminen <nospam@thanks.invalid> - 2011-09-15 06:46 +0000
Re: static constants in a class Marc <marc.glisse@gmail.com> - 2011-09-15 07:59 +0000
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-15 20:09 +1200
Re: static constants in a class Marc <marc.glisse@gmail.com> - 2011-09-15 08:11 +0000
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-15 21:20 +1200
Re: static constants in a class Marc <marc.glisse@gmail.com> - 2011-09-15 14:03 +0000
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-16 09:32 +1200
Re: static constants in a class Marc <marc.glisse@gmail.com> - 2011-09-16 04:28 +0000
Re: static constants in a class Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-09-15 10:50 +0200
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-15 21:17 +1200
Re: static constants in a class Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-09-15 11:48 +0200
Re: static constants in a class Christopher <cpisz@austin.rr.com> - 2011-09-15 14:24 -0700
Re: static constants in a class Ian Collins <ian-news@hotmail.com> - 2011-09-16 09:32 +1200
Re: static constants in a class Leigh Johnston <leigh@i42.co.uk> - 2011-09-15 22:54 +0100
Re: static constants in a class TonyO <guinness.tony@gmail.com> - 2011-09-15 02:50 -0700
csiph-web