static const help

C

cppsks

"Defining static const variables inside the class is not universally
supported yet, so for now I guess you'll
have to move the definition out of the body of the class.

No, static const inside classes is only allowed for integral consts, like
static const enum and static const int, not for arrays and structs. It does
make sense for an array of integral consts though."


I read the above statements in this group a while back. First off, is that
true? (according to the standard) Secondly, what would be the best way to
declare something that is not_integral a const variable?

For instance:
static const char* hi;

Thanks.
 
V

Victor Bazarov

cppsks said:
"Defining static const variables inside the class is not universally
supported yet, so for now I guess you'll
have to move the definition out of the body of the class.

No, static const inside classes is only allowed for integral consts, like
static const enum and static const int, not for arrays and structs. It does
make sense for an array of integral consts though."


I read the above statements in this group a while back. First off, is that
true? (according to the standard)

Is what true according to the standard? That defining is not universally
supported? That it does make sense? No. True is that you _may_ define
a static data member of an integral type and initialise it right there in
the class definition.
Secondly, what would be the best way to
declare something that is not_integral a const variable?

For instance:
static const char* hi;

What do you mean by "best way"? Is there any _other_ way than the one you
showed?

class Blah {
static const char* hi;
};

const char * Blah::hi = "some initialisation";

V
 
J

John Harrison

cppsks said:
"Defining static const variables inside the class is not universally
supported yet, so for now I guess you'll
have to move the definition out of the body of the class.

No, static const inside classes is only allowed for integral consts, like
static const enum and static const int, not for arrays and structs. It
does
make sense for an array of integral consts though."


I read the above statements in this group a while back. First off, is that
true? (according to the standard) Secondly, what would be the best way to
declare something that is not_integral a const variable?

I think you are mixing up declaration and definition. The above two comments
refer to /defining the value/ of a constant inside the class declaration.
E.g.

class X
{
static const int c = 1;
};

It is true that some compilers do not support this, and it is true that it
is only allowed for integral types. But it is not true that you cannot
declare other types of constant inside a class. For instance this is
perfectly legal, always has been, and should be accepted by any compiler

class X
{
const double x;
static const double y;
};
For instance:
static const char* hi;

That is not a const variable. It's a variable pointing to constant char.
Perhaps you meant this

static char* const hi;

or this

static const char* const hi;

But in any case the only way do declare something const is to put the word
const (in the right place) in the declaration. That's the best way, I'm not
sure what other way you had in mind.

john
 
J

JKop

cppsks posted:
"Defining static const variables inside the class is not universally
supported yet, so for now I guess you'll
have to move the definition out of the body of the class.

No, static const inside classes is only allowed for integral consts,
like static const enum and static const int, not for arrays and
structs. It does make sense for an array of integral consts though."


I read the above statements in this group a while back. First off, is
that true? (according to the standard) Secondly, what would be the best
way to declare something that is not_integral a const variable?

For instance:
static const char* hi;

Thanks.


Here's how I'd do it:


//blah.hpp

class Monkey
{
public:

static double const r;
};



//blah.cpp

double const Monkey::r = 67.2;


-JKop
 
A

Andrey Tarasevich

cppsks said:
"Defining static const variables inside the class is not universally
supported yet, so for now I guess you'll
have to move the definition out of the body of the class.

No, static const inside classes is only allowed for integral consts, like
static const enum and static const int, not for arrays and structs. It does
make sense for an array of integral consts though."

I read the above statements in this group a while back. First off, is that
true? (according to the standard)

The first part of the quoted statement is terminologically incorrect
(and the second part in not clear since it seems to taken out of
context). It is never possible to _define_ a static const member inside
class definition. Data members are only _declared_ in class definition.
Even if this declaration includes an initializer (which is allowed for
data members of integral and enum type) it is still only a declaration,
not a definition. If the program requires this static data member to be
defined, the definition must appear outside of the class definition.
Secondly, what would be the best way to
declare something that is not_integral a const variable?

For instance:
static const char* hi;

Declare? Well, just like you said yourself, include a

static const char* hi;

inside class definition and you have your declaration.

Now if you also need to define it, the definition must appear outside

const char* MyClass::hi;

and might also include an initializer

const char* MyClass::hi = "Hello World!";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top