473,623 Members | 3,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5416
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*****@hotmai l.com> wrote in message
news:cl******** **@news.lsil.co m...
"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 terminologicall y 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
28192
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
5747
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 understanding of C++. I would now like to get it right once and for all, if possible. Most severe is the problem illustrated by the code below. It's based on the "pluggable factory pattern" described in http://www.adtmag.com/joop/crarticle.asp?ID=1520
14
2858
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
2881
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: static const int type = 0; };
9
8878
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 declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
14
6004
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 declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
3
2042
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 menu's should then be able to interact with each other. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
6
5161
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; template <struct Lookup<0,0{ // some typedef + enums definitions static const char A; };
7
1666
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 class freqoffset { public: freqoffset():offset(0),strength(0){}; int set(double offset,double strength); double getOff() const{return offset;};
1
8317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4067
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.