Using enum inside types - C4482 C ++ Compiler Warning

I use the full rename name inside the method in one of my classes. But I get a compiler warning saying: "C4482 warning: non-standard extension is used: enum" Foo "is used in a qualified name." In C ++, do we need to use enumerations without a qualified name? But imo that looks ugly.

Any thoughts?

+56
c ++ enums compiler-warnings
Feb 05 '09 at 1:38
source share
7 answers

Yes, enumerations do not create a new "namespace"; values ​​in an enumeration are directly accessible in the environment. So you get:

enum sample { SAMPLE_ONE = 1, SAMPLE_TWO = 2 }; int main() { std::cout << "one = " << SAMPLE_ONE << std::endl; return 0; } 
+52
Feb 05 '09 at 1:46
source share

To make it clean, replace:

 enum Fruit { ORANGE = 0, BANANA = 1 }; 

from

 namespace Fruit { enum { //no enum name needed ORANGE = 0, BANANA = 1 }; }; ... int f = Fruit::BANANA; //No warning 
+35
Aug 20 '11 at 13:25
source share

While he answers the question, he did not take into account how I always used transfers. Despite the fact that they are simply more or less names for numbers, I always used them to define types that can have only certain values.

If an enumeration is part of a class, this helps consumers clearly identify the enumeration link:

 class Apple { enum Variety { Gala, GoldenDelicious, GrannySmith, Fuji } ... }; 

Then consumers will be able to declare enumeration instances, pass as parameters and define them when referring to one of the types.

 unsigned int GetCountOfApples( Apple::Variety appleVariety ); ... fujiCnt = GetCountOfApples( Apple::Fuji ); 

Sometimes you want to list outside a class or two enumerations in the same class, and you can do something like what Poyo had. However, you cannot refer to the type of enumeration, so just name it.

 namespace Color { enum ColorEnum { Blue, Red, Black }; 

Now using enumeration and values ​​will work as follows:

 Color::ColorEnum firstColor = Color::Blue; Color::ColorEnum secondColor = Color::Red; if( firstColor == secondColor ) .... 

Now, if they have different enumerations with the same name, they will always match the type. Then you could handle the question that Gambor is asking about.

 BananaColorEnum banCol = BananaColor::Yellow; TomatoColorEnum tomCol = TomatoColor::Yellow; 
+14
Apr 13 2018-12-12T00:
source share

Yes. Conceptually, an enumeration defines the type and possible values ​​of this type. Although it seems natural, define enum foo { bar, baz }; , and then referring to foo::baz is the same as calling int::1 .

+11
Feb 05 '09 at 19:59
source share
 namespace Company { typedef int Value; enum { Microsoft= 0, APPLE = 1, }; }; namespace Fruit { typedef int Value; enum { ORANGE = 0, BANANA = 1, APPLE = 2, }; }; ... Fruit::Value f = Fruit::BANANA; //No warning Company::Value f = Company::APPLE; //is different value then Fruit::APPLE 

This works with the GCC compiler and MS and Mac. The advantage is that you can use the namespace operator and pass conflicts. A small flaw is that instead of Fruit you need to write Fruit :: Value. this is more useful in a large project when you don’t know which enums are in another class.

If C ++ 11 can be used instead, this is much simpler, because then the enum :: namespace syntax is possible.

+8
Sep 11 '12 at 19:37
source share

The cleanest way I've found to do this is to define an enumeration as such

 namespace Samples { enum Value { Sample1, Sample2, Sample3 }; } typedef Samples::Value Sample; 

Then in function and variable definitions you can use typedef:

 void Function(Sample eSample); Sample m_eSample; 

And in your .cpp file, you can use the namespace to assign variables:

 void Function(Sample eSample) { m_eSample = Samples::Sample1; eSample = Samples::Sample2; } 
+7
Aug 08 2018-12-12T00:
source share

Personally, I think this is a compiler error. I use C ++ a lot of time. Unfortunately, the OP does not have sample code. People's interpretation of Java listing was actually the right iMO. Mine, there was such a ...

 class Foo { enum tMyEnum { eFirstVal = 0, eSecondVal = 1}; // ... tMyEnum m_myVal; }; void Foo::MyMethod() { if(m_myVal == tMyEnum::eFirstVal) { // ... } } 

I also tried Foo :: tMyEnum :: eFirstVal. Without qualifiers, everything is compiled.

+1
Oct 19 '15 at 18:16
source share



All Articles