javalike constants in C++

Started by
17 comments, last by anonuser 18 years, 10 months ago
What compiler are you using, encom?
Advertisement
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 };};
moe.ron
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.
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]
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 =)
moe.ron
By the way: your demonstrated Java "constants" are NOT constants.

You want public static final int A = 2;.
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
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.
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.

This topic is closed to new replies.

Advertisement