innuendomaximus
innuendomaximus

Reputation: 353

Set a structs' value to one of five possible

My classes' struct needs to be one of five values. Right now it looks like this:

// Bar.h  
struct Foo{   
 string value;
}
class Bar{    
Bar(); 
}

// Bar.cpp
Bar::Bar(//magic here){
// or here
}

I want to set Foo to one of five values/strings as it is now (VALUEA, VALUEB,...) in the constructor, and only those five should be allowed. Its not important if the type is a string or a bool, since I just do checks on Foo if it its value == x.

How can I force the programmer to use one of the types?

Upvotes: 0

Views: 103

Answers (1)

Christian Hackl
Christian Hackl

Reputation: 27538

Use an enum class, not a struct.

enum class Foo {
    ValueA,
    ValueB,
    ValueC,
    ValueD,
    ValueE
};

Upvotes: 5

Related Questions