Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static constants in a class

Reply
Thread Tools

static constants in a class

 
 
TonyO
Guest
Posts: n/a
 
      09-15-2011
On Sep 14, 10:43*pm, Urs Thuermann <(E-Mail Removed)> wrote:
> How should one define a constant, say of type int, in a class?


<snip>

---- foo.hh ----
class Foo {
public:
static const int N = 10; // initialise here, in declaration
...
};
---- foo.cc ----
#include "foo.hh"
const int Foo::N; // single definition - no initialiser required
---- bar.cc ----
#include "foo.hh"
void f() {
int array[Foo::N]; // remember to qualify the name when used
for (int i = 0; i < Foo::N; i++) { ... }
----------------
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      09-15-2011
Ian Collins wrote:

> On 09/15/11 08:11 PM, Marc wrote:
>> Ian Collins wrote:
>>
>>> On 09/15/11 07:59 PM, Marc wrote:
>>>> What happens if you move the "= 10" from where it is to the "static"
>>>> line, keeping all the rest unchanged?
>>>
>>> He answered that in the part of the post you snipped....

>>
>> No, he had removed the declaration in foo.cc.

>
> He wrote
>
> 2. class Foo {
> public:
> static const int N = 10;
> ...
> };


I said: "move". So in foo.h:
struct Foo {
static const int N = 10;
};

and in foo.cc:
const int Foo::N;

> N can only be initialised once.


Am I initializing it several times?
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      09-15-2011
On Sep 15, 4:17*am, Ian Collins <(E-Mail Removed)> wrote:
> On 09/15/11 08:50 PM, Alain Ketterlin wrote:
>
>
>
>
>
> > Urs Thuermann<(E-Mail Removed)> *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.
>
> --
> Ian Collins- Hide quoted text -
>
> - Show quoted text -



Where did the OP require it to be a _compile_ time constant? He just
said constant. The FAQ leads one to believe this is the proper way to
define a constant that is accessable outside of a class, in order to
avoid the initialization fiasco. I suppose any solution to define a
_compile_ time constant does not avoid the initialization fiasco
problem, except using #define...

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

If the constant is to not be accessable outside the class, then one
would just declare a non static const member varibale and initialize
it in the initialization list.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-15-2011
On 09/16/11 09:24 AM, Christopher wrote:
> On Sep 15, 4:17 am, Ian Collins<(E-Mail Removed)> wrote:
>> On 09/15/11 08:50 PM, Alain Ketterlin wrote:
>>> Urs Thuermann<(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-15-2011
On 09/16/11 02:03 AM, Marc wrote:
> Ian Collins wrote:
>
>> On 09/15/11 08:11 PM, Marc wrote:
>>> Ian Collins wrote:
>>>
>>>> On 09/15/11 07:59 PM, Marc wrote:
>>>>> What happens if you move the "= 10" from where it is to the "static"
>>>>> line, keeping all the rest unchanged?
>>>>
>>>> He answered that in the part of the post you snipped....
>>>
>>> No, he had removed the declaration in foo.cc.

>>
>> He wrote
>>
>> 2. class Foo {
>> public:
>> static const int N = 10;
>> ...
>> };

>
> I said: "move". So in foo.h:
> struct Foo {
> static const int N = 10;
> };
>
> and in foo.cc:
> const int Foo::N;
>
>> N can only be initialised once.

>
> Am I initializing it several times?


I misunderstood your original post.

--
Ian Collins
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      09-16-2011
Ian Collins wrote:

> I misunderstood your original post.


That'll teach me to try and save time by not writing 2 lines
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
referring to static fields of enclosing class from static inner class? bugbear Java 4 08-23-2006 08:26 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Inheritance and static protected constants Collin VanDyck Java 8 10-22-2003 03:54 PM



Advertisments