Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10184

Re: static constants in a class

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: static constants in a class
Date 2011-09-16 09:32 +1200
Message-ID <9df975F2gmU15@mid.individual.net> (permalink)
References <ygfboumyf3c.fsf@janus.isnogud.escape.de> <87d3f2xk8t.fsf@dpt-info.u-strasbg.fr> <9ddu5qF2gmU2@mid.individual.net> <55639b4f-449a-470a-9438-80577304e136@k7g2000vbd.googlegroups.com>

Show all headers | View raw


On 09/16/11 09:24 AM, Christopher wrote:
> On Sep 15, 4:17 am, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 09/15/11 08:50 PM, Alain Ketterlin wrote:
>>> Urs Thuermann<u...@isnogud.escape.de>    writes:
>>
>>>> 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;
>>>> ----------------
>>> [...]
>>
>>> Make it a inline static method:
>>
>>>       class Foo {
>>>           ...
>>>           inline static int N() { return 10; }
>>>           ...
>>>       };
>>
>> The inline is unnecessary, but even without it the solution isn't valid:
>> Foo::N() isn't a compile time constant.
>
> Where did the OP require it to be a _compile_ time constant?

---- bar.cc ----
     void f() {
         int array[N];
         for (int i = 0; i < N; i++) { ... }
----------------

> I do not understand the template solution proposed, what are the
> internal workings that make it advantagous?

It was unnecessarily complex.

The correct solution was the one posted by "Marc".

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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