 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cppsks Guest
|
Posted: Tue Oct 26, 2004 8:27 pm Post subject: static const help |
|
|
"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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Oct 26, 2004 10:29 pm Post subject: Re: static const help |
|
|
cppsks wrote:
| Quote: | "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.
| Quote: | 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Oct 27, 2004 7:07 am Post subject: Re: static const help |
|
|
"cppsks" <sksjava (AT) hotmail (DOT) com> wrote
| Quote: | "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;
};
| Quote: |
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
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Wed Oct 27, 2004 8:12 am Post subject: Re: static const help |
|
|
cppsks posted:
| Quote: | "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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Wed Oct 27, 2004 5:55 pm Post subject: Re: static const help |
|
|
cppsks wrote:
| Quote: | "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.
| Quote: | 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!";
--
Best regards,
Andrey Tarasevich
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|