将结构的值设置为五种可能的值之一

pus*_*ebp 0 c++ struct

我的类的结构需要是五个值之一.现在它看起来像这样:

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

// Bar.cpp
Bar::Bar(//magic here){
// or here
}
Run Code Online (Sandbox Code Playgroud)

我想在构造函数中将Foo设置为现在的五个值/字符串之一(VALUEA,VALUEB,...),并且只允许这五个值.如果类型是字符串或bool,它并不重要,因为我只是检查Foo,如果它的值== x.

如何强制程序员使用其中一种类型?

Chr*_*ckl 5

使用enum class,而不是struct.

enum class Foo {
    ValueA,
    ValueB,
    ValueC,
    ValueD,
    ValueE
};
Run Code Online (Sandbox Code Playgroud)