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 

C++ style constants

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





PostPosted: Tue Nov 23, 2004 6:54 pm    Post subject: C++ style constants Reply with quote



Could anyone remind of the right way to declare a bunch of integral
and textual constants in the scope of a C++ class? I prefer not to use
#define but global consts in include files cause duplicate symbols at
link-time (of course!) Also, I see no reason why each and every
instance of the given class should have its own copy of these
constants. Should I make them static?

#define may be unsafe but it's simple. Now what's the right, and
hopefully simple C++ way to do that?

Cheers

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





PostPosted: Wed Nov 24, 2004 9:20 am    Post subject: Re: C++ style constants Reply with quote



"UncleSeb" <sebastien.wailliez (AT) gmail (DOT) com> wrote...
Quote:
Could anyone remind of the right way to declare a bunch of integral
and textual constants in the scope of a C++ class? I prefer not to use
#define but global consts in include files cause duplicate symbols at
link-time (of course!) Also, I see no reason why each and every
instance of the given class should have its own copy of these
constants. Should I make them static?

You probably should. You can even initialise them right there, in the
class definition if your compiler supports it (some older ones don't).
Or, you could use 'enum' for that. Unnamed enumeration would suffice.

Quote:
#define may be unsafe but it's simple. Now what's the right, and
hopefully simple C++ way to do that?

class HasConstants {
enum { privateConstOne = 1, privateConstTwo = 2 };
public:
enum { publicConstOne = 1, publicConstTwo = 2 };
};

int main() {
int HCCO = HasConstants::publicConstOne; // implicit conversion
// from an enumerator to
// int is used here.
}

V


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

Back to top
Bob Hairgrove
Guest





PostPosted: Wed Nov 24, 2004 9:29 am    Post subject: Re: C++ style constants Reply with quote



On 23 Nov 2004 13:54:59 -0500, [email]sebastien.wailliez (AT) gmail (DOT) com[/email] (UncleSeb)
wrote:

Quote:
Could anyone remind of the right way to declare a bunch of integral
and textual constants in the scope of a C++ class? I prefer not to use
#define but global consts in include files cause duplicate symbols at
link-time (of course!) Also, I see no reason why each and every
instance of the given class should have its own copy of these
constants. Should I make them static?

#define may be unsafe but it's simple. Now what's the right, and
hopefully simple C++ way to do that?

In all files or headers which use the constant, write:

extern const int MyNumber;

In just *one* .cpp file in your project, you define it (doesn't matter
where):

const int MyNumber = 1;

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

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

Back to top
Thomas Hansen
Guest





PostPosted: Wed Nov 24, 2004 8:24 pm    Post subject: Re: C++ style constants Reply with quote



[in main.h file:]
class X
{
const static int Y;
};



[in main.cpp file:]
const int X::Y = 2;

You need to define your static variables ONCE and ONCE ONLY!
Which basically means if the .h file is included in several modules
(cpp files) it will be defined more then once meaning you will get
multiple X::Y variables which will resolve to a linking error (multiple
definitions of X::Y)

Therefor you DECLARE your static variables in the .h file and you
DEFINE them ONCE AN ONCE ONLY in a module (cpp file)

It's the same for other static objects the only place it becomes
different is in template classes
In template classes you can't (unless you have support for "export" in
your compiler) define static members in a module file (cpp file)
therefor you must define them in a .h file
Example:

[in main.h]
template<class T>
class Z
{
const static int Y;
};

template<class T>
const int Z<T>::Y = 2;


(yes I realize this is simplification since you can declare your
template class in cpp file but didn't want to confuse OP...)
Thomas Hansen


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





PostPosted: Thu Nov 25, 2004 2:05 am    Post subject: Re: C++ style constants Reply with quote

"UncleSeb" <sebastien.wailliez (AT) gmail (DOT) com> wrote


Quote:
Could anyone remind of the right way to declare a bunch of integral
and textual constants in the scope of a C++ class? I prefer not to use
#define but global consts in include files cause duplicate symbols at
link-time (of course!)

Of course not. Soemthing strange is goingon there.

const int Alpha = 1;

in a header shall work all the way -- const makes the default linkage
static, so that will create its own Alpha for every object file. But in
practice the compilers will just use the value, and not create a variable at
all. (Unless you take its address -- in that case it is created, and you
shall be aware that you'll see different addresses in different modules. So
that works until you treat the stuff as value not as identity.)

Similar for any other types.

Newer compilers allow you to add such members as class statics (but only
integrals are allowed with prompt init), and some books suggest against the
enum hack for that reason; IMHO unnamed enums are great for the purpose and
work everywhere.

Quote:
#define may be unsafe but it's simple. Now what's the right, and
hopefully simple C++ way to do that?

#define has just one fine property: compiler ignore repeated identical
#defines. While having multiple identical consts will trigger an error at
compile time.

Unless you're hit by that problem I don't use #define for constants, except
for really special cases like to have a string that can be used in other
strings.



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

Back to top
Alex Vinokur
Guest





PostPosted: Thu Nov 25, 2004 8:43 am    Post subject: Re: C++ style constants Reply with quote


"UncleSeb" <sebastien.wailliez (AT) gmail (DOT) com> wrote

Quote:
Could anyone remind of the right way to declare a bunch of integral
and textual constants in the scope of a C++ class? I prefer not to use
#define but global consts in include files cause duplicate symbols at
link-time (of course!)
----------------------------------------------
Also, I see no reason why each and every
instance of the given class should have its own copy of these constants.

Sometimes it is necessary.

struct Foo
{
static int counter_s;
const int id_;
Foo() : id_ (counter_s++) {}
};
int Foo::counter_s(0);

----------------------------------------------

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn







[ 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.