Re: Aliasing enums with typedef
On 30 Okt., 23:35, JoshG <Inbi...@gmail.com> wrote:
Hi There,
I was wondering if someone could help me with a language/syntax issue
I'm having, any suggestions are welcome.
Basically I have the following situation:
I am using a library where the following class resides: (Note: this is
a library class, so I can not modify it)
class LibraryClass
{
public:
enum ItemType
{
TYPE_A, TYPE_B
}
Missing trailing ;
};
I am trying to write a class that uses this library class in the
following way
class MyClass
{
typedef enum LibraryClass::ItemType ItemType;
void PrintType(ItemType type)
{
switch(type)
{
case TYPE_A:
print("typeA"); break;
case TYPE_B:
print("typeB"; break;
}
One missing parentheses. Do you mean printf here?
}
};
Now, naturally the compiler complains that "TYPE_A" is not a member of
MyClass. So I do the following:
case ItemType::TYPE_A:
Which is invalid anyway. Classic C enums have no enum namespace.
The proper qualification would be LibraryClass::TYPE_A and
LibraryClass::TYPE_B.
Which I am quite happy to do. But this now generates a warning:
warning C4482: nonstandard extension used: enum
'LibraryClass::ItemType' used in qualified name
The compiler is right. C++0x will add scoped enumeration types,
which support qualified enumerators, but classic enums are unscoped.
Because ItemType is member of class LibraryClass, each enumerator
must be qualified with the class name.
Some of the requirements for this code is that it compiles without
warnings (at any level), and is cross platform compatible.
Now, I am using Microsoft Visual Studio 2005, and I know some might
say this is my problem. But the fact remains that I need a
standardised way to do this, and any practical workarounds that work
over all platforms would be much appreciated.
Fix the code as explained and it should be fine then.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]