C++(34)-enum-c++11
enum是常用的。
1.默认数据类型:为整形变量 int ,unsigned int,char 不可以为float。
2.默认初值:从0开始,后面的值++,如果不是先赋值。
3.枚举值作用域: 同一个cpp里,枚举值不可以同名。
#include <iostream>
#include <stdlib.h>
using namespace std;
//枚举是一个内存空间
//1. 默认为int型,值从0 开始的 后值++
//1.1. 只有不从0开始时才需要赋值
//1.2 区域外的值会报错 EXample m_value =5;
enum EXample
{
A,B,C
};
//2.整数以外的类型 : 可以为unsigned char ,char 但不可以为float
//2.1枚举值不能同名
enum EXampleuchar : char
{
//A,B,C 编译不通过
D,E,F
};
//3.log 实例
class Log
{
/* 取代了3个变量
const int logLeverError=0;
const int logLeverWarning=1;
const int logLeverInfo=2;
int m_logLevel=Info;//当前的日志级别 初始化
*/
// 避免命名冲突:枚举值全部大写,作用域为public
public:
enum Level
{
LEVEL_ERROR=0,LEVEL_WARNING,LEVEL_INFO
};
Level m_loglevel=LEVEL_INFO;
public:
void SetLevel(Level level)
{
m_loglevel=level;
}
void Error(const char* message)
{
if(m_loglevel>=LEVEL_ERROR)
{
cout<<"[Error]:"<<message<<std::endl;
}
}
void Info(const char* message)
{
if(m_loglevel>=LEVEL_INFO)
{
cout<<"[Info]:"<<message<<std::endl;
}
}
void Warn(const char* message)
{
if(m_loglevel>=LEVEL_WARNING)
{
cout<<"[warning]:"<<message<<std::endl;
}
}
};
int main()
{
Log log;
log.SetLevel(Log::LEVEL_INFO);//此处为枚举值
log.Warn("hello");
log.Error("hello");
log.Info("hello");
cout<<"hello"<<endl;
EXampleuchar m_value =D;
cout<<m_value<<endl;
system("pause");
return 0;
}
常用的。

浙公网安备 33010602011771号