473,397 Members | 1,961 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,397 software developers and data experts.

Initialized Constants

Hello all,
I have a quick question coming from a long time C programmer, trying to
get back into C++. What I want to do is something like (C version):

int OpenLog(char *Filename)
{
FILE *stream;

if((stream=fopen(Filename,"w+"))==NULL)
return(ERROR);

fclose(stream);
return(OK);
}

Obviously this doesn't do much. The key lines are the two return()'s
that would have ERROR and OK defined in some "constants" header file.

In C++, I know you can create static functions that do not require you
to instantiate the class. However, is there a way to do this with
variables? I'd want to initialize them in the header file as well. So
far, all I've come up with is something like:

class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

I can reference Constants::ReturnCodes::Error w/o instantiating the
class. Is there another way to do it?

Thanks.
Jun 27 '06 #1
8 1773
Brian C wrote:
I have a quick question coming from a long time C programmer, trying
to get back into C++. What I want to do is something like (C version):

int OpenLog(char *Filename)
{
FILE *stream;

if((stream=fopen(Filename,"w+"))==NULL)
return(ERROR);

fclose(stream);
return(OK);
}

Obviously this doesn't do much. The key lines are the two return()'s
that would have ERROR and OK defined in some "constants" header file.

In C++, I know you can create static functions that do not require you
to instantiate the class. However, is there a way to do this with
variables? I'd want to initialize them in the header file as well. So
far, all I've come up with is something like:

class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

I can reference Constants::ReturnCodes::Error w/o instantiating the
class. Is there another way to do it?


That's indeed a decent way of dealing with constants.

You can have separate static const ints in the same class. You can
replace 'class' with 'namespace' (and drop 'public' and the closing
semicolon), and then you don't need the keyword 'static':

namespace Constants {
{
enum ReturnCodes { Error = -1, Ok = 0 };
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '06 #2
Brian C posted:

class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

If they're integer types, then you can initialise them within the class's
definition:
class Constants {
public:

static unsigned const magic_number = 42;

};
If they're something other than integer types, then you'll have to define
them in a source file as follows:
class Constants {
public:

static char * const address;

};

char * const Constants::address = 0; /* This line in source file */

Victor's idea of namespaces may also be viable.

--

Frederick Gotham
Jun 27 '06 #3
Frederick Gotham wrote:
Brian C posted:

class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

If they're integer types, then you can initialise them within the class's
definition:
class Constants {
public:

static unsigned const magic_number = 42;

};
If they're something other than integer types, then you'll have to define
them in a source file as follows:
class Constants {
public:

static char * const address;

};

char * const Constants::address = 0; /* This line in source file */


It is inconvenient to maintain both the declaration and the definition
of a single const variable in two separate places. So I would just
consolidate them both into one place - and not within a class but
within a namespace.

Note also that a const variable is, by default, static (that is, it has
internal linkage) - so the keyword static would be redundant for this
type of declaration. Here's an example:

namespace Constants
{
const double kPi = 3.14159265358979323846264338327950288;
}

Greg

Jun 27 '06 #4
Frederick Gotham wrote:
Brian C posted:

class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

If they're integer types, then you can initialise them within the class's
definition:
class Constants {
public:

static unsigned const magic_number = 42;

};
If they're something other than integer types, then you'll have to define
them in a source file as follows:
class Constants {
public:

static char * const address;

};

char * const Constants::address = 0; /* This line in source file */

Victor's idea of namespaces may also be viable.

Yes, I was playing with what Victor had suggested with the namespaces,
but I was using const static int's, which is kind of what you're
suggesting, and the compiler would not let me use them w/o instantiating
the class which is not what I wanted.

I do see the difference though with what I was trying w/ something like:

class Constants
{
public:
const static int Error = -1;
};

Seems you guys are putting static first. I'll have to check out the FAQ
because that is a quite annoying thing.

Thanks all.
Jun 27 '06 #5
Brian C posted:

class Constants
{
public:
const static int Error = -1;
};

Seems you guys are putting static first. I'll have to check out the
FAQ because that is a quite annoying thing.

Freedom of expression: You can put the words in (pretty much) any order
you like.

The following two definitions are IDENTICAL:

int const a = 4;
const int a = 4;

as are the following two:

const char * const p = 0;
char const * const p = 0;

as are the following six:

const int static error = 5;
const static int error = 5;
int const static error = 5;
int static const error = 5;
static const int error = 5;
static int const error = 5;
--

Frederick Gotham
Jun 27 '06 #6
Brian C wrote:
...
class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

I can reference Constants::ReturnCodes::Error w/o instantiating the
class. Is there another way to do it?
...


Well, you probably know that already, but you can't really reference it
like that. The qualified name for the above 'Error' constant is
'Constants::Error', not 'Constants::ReturnCodes::Error'.

--
Best regards,
Andrey Tarasevich
Jun 27 '06 #7
Frederick Gotham wrote:
Brian C posted:

class Constants
{
public:
const static int Error = -1;
};

Seems you guys are putting static first. I'll have to check out the
FAQ because that is a quite annoying thing.

Freedom of expression: You can put the words in (pretty much) any order
you like.

The following two definitions are IDENTICAL:

int const a = 4;
const int a = 4;

as are the following two:

const char * const p = 0;
char const * const p = 0;

as are the following six:

const int static error = 5;
const static int error = 5;
int const static error = 5;
int static const error = 5;
static const int error = 5;
static int const error = 5;

Frederick,
Ok, I must've been confusing it with something else.

Thanks.
Jun 27 '06 #8
Andrey Tarasevich wrote:
Brian C wrote:
...
class Constants
{
public:
enum ReturnCodes
{
Error=-1,
Ok=0
};

....etc...
};

I can reference Constants::ReturnCodes::Error w/o instantiating the
class. Is there another way to do it?
...


Well, you probably know that already, but you can't really reference it
like that. The qualified name for the above 'Error' constant is
'Constants::Error', not 'Constants::ReturnCodes::Error'.

Andrey,
Thanks. It does work however, I did notice that both worked, and I
wondered why. I'll have to try it under gcc, but VC++ lets you access it
either way.
Jun 27 '06 #9

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

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
2
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...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
5
by: Arno | last post by:
Hello, here is my problem: I have a complex data type, like a std::vector< std::string > and need some global constant values of this type. The type is also used non-constant throughout the...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.