Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encapsulating a private enum

Tags:

c++

enums

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.

like image 918
Anonymous Avatar asked Dec 22 '22 07:12

Anonymous


1 Answers

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
like image 179
Pavel Minaev Avatar answered Dec 24 '22 22:12

Pavel Minaev