tags:

views:

230

answers:

2

Previously I've defined enumerated types that are intended to be private in the header file of the class.

private:
  enum foo { a, b, c };

However, I don't want the details of the enum exposed anymore. Is defining the enum in the implementation similar to defining class invariants?

const int ClassA::bar = 3;
enum ClassA::foo { a, b, c };

I'm wondering if this the correct syntax.

+3  A: 

C++ doesn't have forward declarations of enums, so you can't separate enum "type" from enum "implementation".

The following will be possible in C++0x:

// foo.h
class foo {
   enum bar : int; // must specify base type
   bar x; // can use the type itself, members still inaccessible
};

// foo.cpp
enum foo::bar : int { baz }; // specify members
Pavel Minaev
A: 

No, enum ClassA::foo { a, b, c }; is not correct syntax.

If you want to move the enum out of the header and into the implementation (.cpp) file, then just do that. If you want to use the enum for parameter types of methods of the class, then you cannot move it, so just leave it private.

Roger Pate