Using class enumerations (C ++)

I am using a library with enumerated classes. Here is an example

class TGNumberFormat
{
  public:
  // ...
  enum EAttribute {   kNEAAnyNumber
    kNEANonNegative
    kNEAPositive
  };
  enum ELimit {   kNELNoLimits
    kNELLimitMin
    kNELLimitMax
    kNELLimitMinMax
  };
  enum EStepSize {   kNSSSmall
    kNSSMedium
    kNSSLarge
    kNSSHuge
  };
  // etc...
};

In the code I have to refer to them as TGNumberFormat::kNEAAnyNumber, for example. I write a GUI that uses these values ​​very often and the code gets ugly. Is there a way to import these listings and just type kNEAAnyNumber? I really do not expect any of these names to overlap. I have tried various ways to use the keyword usingand no one will compile.

+5
source share
4 answers

, , . using . , . ,

namespace TGEnumerators
{
    static EAttribute const kNEAAnyNumber(TGNumberFormat::kNEAAnyNumber);
    // etc.
}

typedef TGNumberFormat , . ,

typedef TGNumberFormat NF;
NF::EAttribute attribute = NF::kNEAAnyNumber;

, , , . , .

+8

++ 11, auto :

//the compiler will see auto and know to use: TGNumberFormat::EAttribute
auto attribute = TGNumberFormat::kNEAAnyNumber;

: g++ -std = ++ 0x -o main main.cpp

++ 11, typedefs, @James McNellis
, - typedefs .

0

Another way that is possible, but involves a bit more work, is to define a batch of constants, which you then use:

eg.

const TGNumberFormat::EAttribute AnyNumber = TGNumberFormat::kNEAAnyNumber;
const TGNumberFormat::EAttribute NonNegative = TGNumberFormat::kNEANonNegative;
...

attribute = AnyNumber;
0
source

Two solutions:

  • Live with him.
  • #define AnyNumber TGNumberFormat::kNEAAnyNumber

* Performed for the cover ... *

0
source

All Articles