Sign in to follow this  
twoaterisn

"const static member" vs "enum" vs "globals"

Recommended Posts

Dear all, I am currently trying to write my first full featured windows program using wxwidgets and C++. My knowledge isn't all that vast, but I'm learing along the way. So for my question: I am writing a class, which has a lot of members that can only be 2 to 6 possible values, each of them different. eg class InfraRed{ public: ... private: int mXUnits; // = MICROMETERS || SECONDS || INVERSECENTIMETER int mYUnits; // = TRANSMITTANCE || REFLECTANCE || ABSORBANCE || KUBELKAMUNK ... } Now I am struggeling how to define all these constants. I use to do it with a list of const int members, but this is quite cumbersome to maintain, the list is very long and I just don't like how it looks. Furtermore, for each instance of InfraRed, I get all these variables in memory, while I actually only need one copy. I have been looking in to making these integers static data members, but the problem is that I then have to initialise each one of them outside of the class definition (In Infrared.cpp). This solves the problem of having multiple copies, but i still have a very long list of initialisations. I was also looking at enum's, but i read somewhere (and i believe this to be true) that they pollute the global namespace. I suppose you cannot declare an enum type inside a class? Or should I put my class, together with the enums in a new namespace? In short, how is this generally done by people who know what they are doing? politely kneeling, TH Edit: forgot some semicolons.

Share this post


Link to post
Share on other sites
Cannot comment on all your questions, but ...
Quote:
Original post by twoaterisn
I was also looking at enum's, but i read somewhere (and i believe this to be true) that they pollute the global namespace. I suppose you cannot declare an enum type inside a class? Or should I put my class, together with the enums in a new namespace?
A class doesn't build a namespace but a scope around its members, also for enums. Hence

class MyClass {

public:

enum {

A_CONSTANT = 0

};

};
would define a constant. Inside the class writing A_CONSTANT is sufficient to use it. Outside it must be qualified with the class name, like MyClass::A_CONSTANT.

Share this post


Link to post
Share on other sites
Thx for your comment Haegarr,

I see you are using the scope resolution operator (::) to access A_CONSTANT. Do you mean by this that enums are always static?

Share this post


Link to post
Share on other sites
Quote:
Original post by twoaterisn
I see you are using the scope resolution operator (::) to access A_CONSTANT. Do you mean by this that enums are always static?
I'm not an expert w.r.t. the C++ specification, so I may be wrong with the one or other topic:

Enums are not statics. Instead they are named values, where each name in the same enum defines a unique value. There is not even a guarantee that enums are implemented as numbers (so I should have avoided the "=0" in my example above), although almost all compilers do so. Further, IIRC enums are substituted by their values at compilation time, causing the problem that debuggers will not show the names but the (integer) values the compiler has chosen.

The lack of a clear type for enums causes other problems, too. So will

class MyClass {

public:

typedef enum {

A_CONSTANT

};

typedef enum {

A_CONSTANT

};

};
cause a duplicate definition of A_CONSTANT. (I think this is in fact what the other source meant with "pollution of the scope"). And the implementation as integers allows for some operations like casting and comparison that are actually not defined on enums by itelf.


That said, enums are not necessarily at the top of the pops of your possibilities. But the alternatives have their own drawbacks, too.

Share this post


Link to post
Share on other sites
It's up to you actually. By the way you can use enums without polluting the global namespace, just wrap it in the class.

Here's an example using enums:


class Animal
{
public:
enum AnimalType
{
dog, cat, cow, parrot // etc
};

private:
AnimalType type;

public:
Animal(AnimalType _type) : type(_type) {}

/* ... */
};

int main()
{
Animal myAnimal(Animal::cat);
}



So go with enums and don't worry.

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