mlimber wrote:
werasm wrote:
toton wrote:
The otherway I was mentioning looks like
namespace test {
class MyClass {
public:
enum MyEnum{
VALUE1,VALUE2
};
};
}
>
One way is to redefine enums applicable to the scope you are in. In
general, I tend to wrap my enumerated types with classes regardless.
This allows features like forward declaring enumerated types, etc. You
don't need to give the enumeration a name (as far as I know).
struct MyEnum{ enum{
VALUE1,
VALUE2 };
};
- Can now be forward declared to be used as arguments to member
function calls.
class MyEnum;
class x_c{ ... void foo(const MyEnum& ) ... };
This is entirely unnecessary since you can forward-declare enums
without the struct:
enum MyEnum;
void Foo( MyEnum );
enum MyEnum { E1, E2 };
void Foo( const MyEnum e )
{
cout << (e==E1 ? "One" : "Two") << endl;
}
- Can be qualified like:
It doesn't work with gcc. I can not forward declare enum with gcc (4.2)
in my linux or mingw build!. Even visual c++ doesn't allow it ( I
hadn't checked 8.0 version though).
Enum's are free makes a big problem occationally. For important enum, I
usually do a typesafe enum (which is a class !) .
But a lots of time, I need enum simply to define enum without safety.
It is always better to have enum const within its enum scope. The
solution by me or werasm looks as fine as free standing enum. I do not
see any problem there. Many time I also use enum within a class to make
them class member (const static ). They lacks typesafety but otherwise
just ok.
However it is always better to have using MyEnum; and MyEnum::One kind
of thing!
(Those libary do not use namespace or a different namespace for each
enum still use the comvention like enum AlignmentType{ atLEFT, atRIGHT
, atCENTER} ; etc.
If the namespace can be opened in CPP file then that solution is also
good. (I need them in CPP file only, most of the time).
MyEnum::VALUE1 etc...
The disadvantage here compared to namespaces is that you *must* qualify
them. There is no using "using" to get rid of "MyEnum".
Looks better. Most of the time I do not want to get rid of MyEnum. It
is always a good idea to know which enum you are talking. Do you ever
want to get rid of the class name to which it's member variable
belongs? note, enum is not free standing const, it is more than that.
It is the collection of values, which is more important. And it is
important to know them by the collection itself, just like all other
classes! (There are different classes of class, most language do not
implement all, or some. Most signeficant classes of clas type are,
struct (value type class) ,class (ref type class) , enum, collection
(they are different in general, they holds other classes in different
way than normal class do), iterator, static (which holds all static
members, non creatable class), exception (which can be throwed, and
they themselves dont throw on creation!, A big problem with c++
exception, I think not having a language type of exception class, what
will happen when if fails to create a exception class!) , interface
(definition type) , abstract (partially implemented type) ,primitive.
And all category van be of sealed ( final) , and virtual.
Thanks for all of yours valuable time .
abir
Cheers! --M