Sign in to follow this  
encom

javalike constants in C++

Recommended Posts

Hi I'm moving from Java to C++. How to use constants in c++ classes like java : ############# JAVA class Foo { public static int A = 2; public static int B = 3; } class Bar { Bar() { println("res = " + (Foo.A + Foo.B)); // I can use it in a switch case statement } } ############ C++ class Foo (in .H) { public : static const int A; static const int B; } (in .CPP) const int Foo::A = 2; const int Foo::B = 3; class Bar { Bar() { printf("res = %d", Foo::A + Foo::B); // I can use it in a switch case statement } } It doesnt work ! 'Foo' : is not a class or namespace name Bar.cpp : error C2065: 'A' : undeclared identifier Foo.cpp : error C2051: case expression not constant Help me please :)

Share this post


Link to post
Share on other sites
Provided your compiler groks them you can initialize static consts in the class definition. Ex:

class Foo {
public:
static const int A = 2;
static const int B = 3;
};

Share this post


Link to post
Share on other sites
Initialisation like that :

class Foo {
public:
static const int A = 2;
static const int B = 3;
};

doesn work :

error C2258: illegal pure syntax, must be '= 0'
error C2252: 'A' : pure specifier can only be specified for functions

Share this post


Link to post
Share on other sites
if I use that :

class foo{
public:
enum{ A = 4, B = 9 };
};

In class Bar :
Error : 'Foo' : is not a class or namespace name
Error : Bar.cpp : error C2065: 'A' : undeclared identifier

Share this post


Link to post
Share on other sites
In the second case (no pun intended [smile]) you might have typed the foo in the wrong case (C++ is case sensitive). I incorrectly called the class foo when it should have been called Foo.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajones
Try this in a header (.hpp) file:


class Foo
{
public:
static const int A;
};


And the following in the body (.cpp) file


const int Foo::A = 4;


Your allowed to initialize static const integral types inside a class declaration. Its the same thing as an enum..

class foo
{
// totally legal
static const int bar = 12;
static const long baz = 0L;
enum batz{ A = 0, B };
};

Share this post


Link to post
Share on other sites
True, but not all compilers support it. IIRC, MSVC 6 is one that doesnt, for example. But it's not the same as an enum. enums have their own type, and you can't take the address of the identifier for an enum value.

Share this post


Link to post
Share on other sites
Quote:
Your allowed to initialize static const integral types inside a class declaration. Its the same thing as an enum..


Of course, but...

Separating declaration and definition enhances encapsulation, works for all types (not just compile-time integral constants), and is more portable (as pointed out by SiCrane, some compilers won't let you initialize static const integral types inside a class declaration).

I offered the hpp/cpp version as an alternative - not as a 'better' method (I often make use of the enum workaround myself). It's just one more tool in the C++ toolbox - one encom might find useful when his static constants aren't necessarily ints. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
True, but not all compilers support it. IIRC, MSVC 6 is one that doesnt, for example. But it's not the same as an enum. enums have their own type, and you can't take the address of the identifier for an enum value.


I meant that they are the same in that enumerated values are static const integral values. But yes, those are valid differences.

I also wasn't aware that it was compiler specific, I thought that was part of the ISO standard. Learn something new every day =)

Share this post


Link to post
Share on other sites
Quote:
Original post by moeron
I also wasn't aware that it was compiler specific, I thought that was part of the ISO standard. Learn something new every day =)

Sadly, "compiler specific" and "required by the standard" aren't mutually exclusive. Moreso in a world where VC6 is still used.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by moeron
I also wasn't aware that it was compiler specific, I thought that was part of the ISO standard. Learn something new every day =)


No, it's standard alright. It's just that calling MSVC 6 standards compliant is a lot like calling a double quarter pounder with cheese low fat.

Share this post


Link to post
Share on other sites
in the body of the source file for the class you must declare an initial value for the const.

This is because statics are the first things initialized so that anything that uses them won't be accessing something undefined.
This is the case for singleton classes as well

class singleton {
public:
singleton* instance(void) { if(p != null) return p; else { p = new singleton; return p};

private:
static singleton* p;

;
singleton::p = NULL;

without the definition you'd get a linker error about an undefined object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this