C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Enum vs. const variables

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
krishnakanna@rediffmail.c
Guest





PostPosted: Wed Nov 30, 2005 1:50 am    Post subject: Enum vs. const variables Reply with quote



hai
in one of my project i have to avoid # defines to define constants
so i hv chosen public enums inside a class to define the constants.
now am supposed to use const instead of enum. but the compiler am using
is not allowin me 2 use this.

wats the solution for this?

eg

class a {
public:
enum b
{
FILE = 0x1,
NO_FILE = 0x2
};
}
should be changed as
class a
{
public:
static const int FILE ;
static const int NO_FILE;
};
and defined the values outside the class
but when i tried to use these const variables somewhere outside this
class then it throws an error as follows..

a.h(46) : error C2057: expected constant expression
a.h(46) : warning C4200: nonstandard extension used : zero-sized array
in struct/union
a.h(1757) : error C2229: class 'b' has an illegal zero-sized array

can u pls suggest me a soln for this????


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Nov 30, 2005 11:01 am    Post subject: Re: Enum vs. const variables Reply with quote



[email]krishnakanna (AT) rediffmail (DOT) com[/email] wrote:
Quote:
in one of my project i have to avoid # defines to define constants
so i hv chosen public enums inside a class to define the constants.
now am supposed to use const instead of enum. but the compiler am using
is not allowin me 2 use this.

Take a look at Boost, it contains a workaround for class-static constants
that works sufficiently well even for compilers that don't support it. That
way you will also find out if you in fact hit a compiler limitation.
However, looking at your code...

Quote:
class a {
public:
enum b
{
FILE = 0x1,
NO_FILE = 0x2
};
}

.... I'd first suggest to drop the use of ALL_UPPERCASE except for macros. In
particular the 'FILE' symbol is like asking for trouble, I could well
imagine a C library that has a macro for its FILE type.

Quote:
should be changed as
class a
{
public:
static const int FILE ;
static const int NO_FILE;
};

No problem up to here.

Quote:
and defined the values outside the class

Also completely valid.

Quote:
but when i tried to use these const variables somewhere outside this
class then it throws an error as follows..

a.h(46) : error C2057: expected constant expression
a.h(46) : warning C4200: nonstandard extension used : zero-sized array
in struct/union
a.h(1757) : error C2229: class 'b' has an illegal zero-sized array

Here is the problem: the size of an array (would have been nice if you had
in fact shown these parts, too) must be a compile-time constant.
However, what you do is declare a constant object which is located somewhere
else. The point is that the code that uses the constant for the array size
needs to know the value at compile time, but it can't do that because the
value is hidden somewhere else.

Quote:
can u pls suggest me a soln for this????

class a
{
static size_t const array_size = 42u;
};

float some_array[a::array_size];

However, not all compilers understand this valid way to use class-static
constants, therefore see the Boost workaround.

Uli



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
werasm
Guest





PostPosted: Wed Nov 30, 2005 11:01 am    Post subject: Re: Enum vs. const variables Reply with quote




[email]krishnakanna (AT) rediffmail (DOT) com[/email] wrote:
Quote:
hai
in one of my project i have to avoid # defines to define constants
so i hv chosen public enums inside a class to define the constants.

The benefit of using static constants as opposed to enums, is that one
can change the value of the constant without changing the definition of
the class that the static constant exists in. This especially holds if
you are only going to use the constants within the definition of the
applicable class (i.e. in the cpp/source file wherein the class is
implemented).

I cannot gather from the standard (5.19 - Constant expressions) whether
the value of the static constant is required for use in a constant
expression - someone?) e.g.

//xC.h
struct xC
{
static const unsigned constMem;
};

//xC.cpp
//Definition of constMem
const unsigned xC::constMem( 10 );

#include "xC.h"
struct yC
{
char name[xC::constMem]; //Is this possible?
};

I have found that compilers were inconsistent - some allowed this,
others did not. They required the literal to be visible in the
compilation unit where the constant was used.

When using the <pimpl idiom>, I prefer enums to static variables as the
definition of the Impl is not visible to the client. Therefore changing
the value of the enum doesn't influence the interface.

Quote:
now am supposed to use const instead of enum. but the compiler am using
is not allowin me 2 use this.

I'm guessing you have not provided a definition for the static
constant, or you are using the static constant in a constant expression
and the compiler requires the literal to be present in the compilation
unit of use.

Quote:
wats the solution for this?

See my example (of xC), it should work. I need to see more of you
source to comment. Note it is important to define xC::constMem as well
apart from declaring.

Regards,

Werner


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Wed Nov 30, 2005 2:43 pm    Post subject: Re: Enum vs. const variables Reply with quote

Ulrich Eckhardt wrote:

[...]
Quote:
class a
{
static size_t const array_size = 42u;
};

float some_array[a::array_size];

However, not all compilers understand this valid way to use
class-static constants, therefore see the Boost workaround.

Are there any recent compilers that don't, or just dinosaurs?
(I know, we sometimes have to use dinosaurs, but it would be
nice to know that the problem won't spring up on us if we move
to new environment, with a new compiler.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Nov 30, 2005 6:13 pm    Post subject: Re: Enum vs. const variables Reply with quote

kanze wrote:
[ support for class-static constants ]
Quote:
Are there any recent compilers that don't, or just dinosaurs?
(I know, we sometimes have to use dinosaurs, but it would be
nice to know that the problem won't spring up on us if we move
to new environment, with a new compiler.)

The dinosaur typically quoted in this context is VC6 but that one is finally
getting extinct, many better alternatives exist. However, I recently
stumbled across another such beast and that is the compilers supplied by MS
that have MS Windows CE as target. These compilers report the same major
version as the one from VC6 and are mostly bug-to-bug compatible. For
those, the only alternative I am aware of is the recently released
development tools for CE5, but I'm not sure if and how these support
earlier variants of CE, and upgrading the OS on those gadgets is often not
an option.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mateusz Loskot
Guest





PostPosted: Thu Dec 01, 2005 3:03 am    Post subject: Re: Enum vs. const variables Reply with quote

Ulrich Eckhardt napisal(a):
Quote:
kanze wrote:
[ support for class-static constants ]
Are there any recent compilers that don't, or just dinosaurs?
(I know, we sometimes have to use dinosaurs, but it would be
nice to know that the problem won't spring up on us if we move
to new environment, with a new compiler.)

The dinosaur typically quoted in this context is VC6 but that one is finally
getting extinct, many better alternatives exist. However, I recently
stumbled across another such beast and that is the compilers supplied by MS
that have MS Windows CE as target. These compilers report the same major
version as the one from VC6 and are mostly bug-to-bug compatible.

Exactly. You are talking about eMbedded Visual C++ 3.0/4.0.
I use this compiler in my every day work and this is a nightmare.
This tool forces C++ programmer to be a very very good acrobatics and
not only know how to design software using C++ features +STL + some
libraries pulled out from boost but also how to do a kind of
"emulation" of standard features which are not supported by compiler in
non-standard way :-)

Quote:
For those, the only alternative I am aware of is the recently released
development tools for CE5, but I'm not sure if and how these support
earlier variants of CE, and upgrading the OS on those gadgets is often not
an option.

Besides better IDE, compiler comming with VS2005 is not much better
than evc++ 4.0.
Don't expect much. There are still mising language features and missing
stuff in the C Run-Time or STL libraries (STLport is recommended for
evc++).

So, eMbedded VC++ is a real dinosaur :-)

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
krish
Guest





PostPosted: Thu Dec 01, 2005 11:04 am    Post subject: Re: Enum vs. const variables Reply with quote

Mateusz Loskot wrote:
Quote:
Ulrich Eckhardt napisal(a):
kanze wrote:
[ support for class-static constants ]
Are there any recent compilers that don't, or just dinosaurs?
(I know, we sometimes have to use dinosaurs, but it would be
nice to know that the problem won't spring up on us if we move
to new environment, with a new compiler.)

The dinosaur typically quoted in this context is VC6 but that one is finally
getting extinct, many better alternatives exist. However, I recently
stumbled across another such beast and that is the compilers supplied by MS
that have MS Windows CE as target. These compilers report the same major
version as the one from VC6 and are mostly bug-to-bug compatible.

Exactly. You are talking about eMbedded Visual C++ 3.0/4.0.
I use this compiler in my every day work and this is a nightmare.
This tool forces C++ programmer to be a very very good acrobatics and
not only know how to design software using C++ features +STL + some
libraries pulled out from boost but also how to do a kind of
"emulation" of standard features which are not supported by compiler in
non-standard way Smile

Yep .... i was using the dinasour VC 6.0 from MS... Even i tried with
the dev c++ frm bloodshed. Both of them were throwing compliation
errors. Later i checked out with a compiler from Green Hills and it is
working fine. Thanks for ur timely support guys..




Quote:
For those, the only alternative I am aware of is the recently released
development tools for CE5, but I'm not sure if and how these support
earlier variants of CE, and upgrading the OS on those gadgets is often not
an option.

Besides better IDE, compiler comming with VS2005 is not much better
than evc++ 4.0.
Don't expect much. There are still mising language features and missing
stuff in the C Run-Time or STL libraries (STLport is recommended for
evc++).

So, eMbedded VC++ is a real dinosaur :-)

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.