472,353 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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.

Jul 22 '05 #1
4 5304
cppsks wrote:
"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
Jul 22 '05 #2

"cppsks" <sk*****@hotmail.com> wrote in message
news:cl**********@news.lsil.com...
"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
Jul 22 '05 #3
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
Jul 22 '05 #4
cppsks wrote:
"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!";

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private:...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object...
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those...
6
by: mathieu | last post by:
Hi there I am trying to provide a lookup from two 'int's into a char array, something like this: template <int g, int estruct Lookup;...
7
by: Jim | last post by:
Hi, I'm trying to build a simple data table for all my classes to use. I don't want to use a vector. The data is stored in class freqoffset ...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.