c++中能否将枚举类型转化为对应的字符串输出

来源:百度知道 编辑:UC知道 时间:2024/07/11 02:31:27
请高手指教,正在编程发先什么都不会,郁闷啊

不能,如果转化你只能自己写,枚举其实就是整型。可以通过switch(值)
case 枚举值:
{ return “对应的字符串”;}
进行转化

不能直接输出,可以定义个字符串数组,根据枚举的值来取

在BCB的VCL中可以实现:
#include <typinfo.hpp>
enum number {One,Two,Three,Four,Five,Six};
class TForm1 : public TForm
{
...
private:
number FNum;
__published:
__property number Num = { read = FNum };
};

.CPP文件
...
void __fastcall TForm1::Button1Click(TObject *Sender)
{
PPropInfo propInfo = GetPropInfo(__typeinfo(TForm1),"Num");
ShowMessage(GetEnumName(*(propInfo->PropType),2)); //显示"Three"
}

switch