473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

A constant integral


When you have a constant integral in a program, like the population of a
town, how do you declare it? A const variable or an enum?

Up until now I've always used variables, e.g.:

unsigned const population_dublin = 8274816;
I'm flipping through pages of Bjarne's book though at the moment, and
enum's seem pretty handy, as in:

enum { population_dublin = 8274816 }
Which style would you advocate?

-Tomás
Mar 3 '06 #1
1 2060
Tomás wrote:
When you have a constant integral in a program, like the population of a
town, how do you declare it? A const variable or an enum?
Whatever floats your boat.
Up until now I've always used variables, e.g.:

unsigned const population_dublin = 8274816;
Is it really 'const'? I wonder, nobody is born and nobody dies in Dublin?
I'm flipping through pages of Bjarne's book though at the moment, and
enum's seem pretty handy, as in:

enum { population_dublin = 8274816 }
Which style would you advocate?


The former unless you need to place it in a class (as 'static') and your
compiler is too old to support integral static const initialisation in the
class definition.

V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #2

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

Similar topics

3
by: Keith | last post by:
I receive compiler error messge: "integral constant expression expected" for: somefunc(char *bar) { char foo; } Is there any way around this? I am using SunWorkStop 5.0.
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define...
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
13
by: Szabolcs | last post by:
Is the following legal? void fun(int N) { int arr; } gcc and Digital Mars accept it, but msvc complains that it wants a constant expression.
8
by: johnehein | last post by:
#include <vector> using namespace std; template <typename Iter> int foo(Iter first, Iter last, int nn) { const size_t n = last - first; double buf; return 0;
3
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.