473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How is "static const int" better than "static enum"?

can not the "static const int" be replaced by "static enum" anywhere?

is it necessary that define special initialization syntax for "static const int"?
Jul 22 '05 #1
3 12358
Ajax Chelsea wrote:
can not the "static const int" be replaced by "static enum" anywhere?
enum is a type, not a variable, so it needs no 'static' storage category.

'int' has an implementation-defined size, and its type is compatible with
variable ints.

'enum' is only guaranteed to have enough bits to store any value used in
their definition.

For a while, compilers could not treat 'static const int' inside a class as
a compile-time constant, and so one couldn't size arrays with it and such.
Using 'enum' as a scalar instead of a typed flag was an easy work-around.
is it necessary that define special initialization syntax for "static

const int"?

?

Constant static data are the only things that can declare inside a class.
This (I suspect) grants them their compile-time constant status.

This is all well-formed, with defined behavior:

class yo { public:
static int z (42);
};

char whatever[yo::z];

But an enum would have worked the same, too.

--
Phlip
Jul 22 '05 #2
Phlip wrote in news:ey******** ***********@new ssvr32.news.pro digy.com:
This is all well-formed, with defined behavior:

class yo { public:
static int z (42);
};

char whatever[yo::z];

But an enum would have worked the same, too.


Also if you also want to use the static integral constant in a
non-compile-time context you also need a definition outside the class,

/* Not in a header file (templates aside)
*/
int yo::z; /* Note no initializer */

int main()
{
int z = yo::z;
int const *zp = &yo::x;
}

enum's don't have this requirment, which is perhapse one way in which
enum's are "better" than static int const's.

Cranking the level of triviality up a notch. An instance of an enum
can also be a static integral constant,

#include <iostream>

struct A
{
enum B { C, D, E };
static B const b = A::E;
};

A::B const A::b;

int main()
{
std::cerr << A::b << "\n";
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Rob Williscroft <rt*@freenet.RE MOVE.co.uk> wrote in message news:<Xn******* *************** ************@19 5.129.110.204>. ..

Cranking the level of triviality up a notch. An instance of an enum
can also be a static integral constant,

#include <iostream>

struct A
{
enum B { C, D, E };
static B const b = A::E;
};

A::B const A::b;

int main()
{
std::cerr << A::b << "\n";
}

Rob.


so I consider that it is not necessary to specialize syntax of "static
const int(long...)", haha
Jul 22 '05 #4

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

Similar topics

2
1548
by: heinquoi | last post by:
hello, i have the code: class window { static const HWND hWnd; static const HINSTANCE hInst; // ... next code here window (HINSTANCE ); }
0
1969
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir must be able to be updated with more pairs later. Here is the solution I have developed so long All my enum-like int values are declared static const inside a namespace as well as a struct with a static map<int,string> to keep the pairs. The...
6
3728
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by "typedef" to contain only either +10 or -10 and the other type named Color declared by "enum" to contain only black, blue, purple, red, white, and yellow.
2
4746
by: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static non-integral data member in a class?
5
7243
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct foo { };
72
4187
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
4
2014
by: babylon | last post by:
I can't do public const short test = new short{1000,2000,3000}; any suggestions? thx
16
10811
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
6
2395
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8465
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
8579
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
7297
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
6158
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
5612
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
4144
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...
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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.