greyer
greyer

Reputation: 97

Can I use an enum in a constructor for a class?

I am trying to make a class and inside of the class I want to define an enum called Type. Can I use the enum I defined as a constructor for my class? If so, how do I access it outside of the class?

class Type {
public:
  enum TType { BLACK, WHITE, GRAY };
  Type(TType type, std::string value);
  ~Type();

...

This code doesn't give me any errors but when I try to create an instance of this class in another class it gives me an error because BLACK is not defined:

Type piece(BLACK, value);

Is there a special way to do this and still have TType as a class constructor for my Type class? This is my first time using enums so I don't know how they work exactly.

Upvotes: 1

Views: 1968

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

Yes you can:

Type piece(Type::BLACK, value);

enum TType is in scope of class Type, so you have to use scope resolution when accessing it outside Type's body and member functions.

Upvotes: 4

Related Questions