主题:一个有关C++位域的程序,感觉有些疑惑,求解
#include<iostream>
using namespace std;
enum Sys {LOW, HIGH}; //系统位数
enum Core {ONE, TWO, FOUR};//cpu单,双,四核
enum Thread {YES, NO}; //是否支持多线程
class Cpu //Cpu类定义
{
public:
Cpu():frequ(0), system(LOW), core(ONE), thread(YES){}
Cpu(int fre, Sys sys, Core co, Thread th):frequ(fre), system(sys), core(co), thread(th){}
void show(void);
private:
unsigned int frequ : 12;
Sys system : 1;
Core core : 2;
Thread thread : 1;
};
void Cpu::show(void)
{
cout<<"the cpu information is:"<<endl;
cout<<"frequency(MHz):"<<frequ<<endl;
cout<<"the system is:"<<endl;
switch(system)
{
case LOW:
cout<<"32bit"<<endl;
break;
case HIGH:
cout<<"64bit"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
cout<<"the core is"<<endl;
switch(core)
{
case ONE:
cout<<"single core"<<endl;
break;
case TWO:
cout<<"double cores"<<endl;
break;
case FOUR:
cout<<"double double cores"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
cout<<"the cpu support over thread:"<<endl;
switch(thread)
{
case YES:
cout<<"yes"<<endl;
break;
case NO:
cout<<"no"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
}
int main(void)
{
Cpu c(2000, HIGH, FOUR, NO);
c.show();
cout<<"the size of the class is:"<<sizeof(Cpu)<<endl;
return 0;
}
调试出来是switch语句有问题,没有执行case,直接执行default,而且sizeof的结果觉得应该是2.
using namespace std;
enum Sys {LOW, HIGH}; //系统位数
enum Core {ONE, TWO, FOUR};//cpu单,双,四核
enum Thread {YES, NO}; //是否支持多线程
class Cpu //Cpu类定义
{
public:
Cpu():frequ(0), system(LOW), core(ONE), thread(YES){}
Cpu(int fre, Sys sys, Core co, Thread th):frequ(fre), system(sys), core(co), thread(th){}
void show(void);
private:
unsigned int frequ : 12;
Sys system : 1;
Core core : 2;
Thread thread : 1;
};
void Cpu::show(void)
{
cout<<"the cpu information is:"<<endl;
cout<<"frequency(MHz):"<<frequ<<endl;
cout<<"the system is:"<<endl;
switch(system)
{
case LOW:
cout<<"32bit"<<endl;
break;
case HIGH:
cout<<"64bit"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
cout<<"the core is"<<endl;
switch(core)
{
case ONE:
cout<<"single core"<<endl;
break;
case TWO:
cout<<"double cores"<<endl;
break;
case FOUR:
cout<<"double double cores"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
cout<<"the cpu support over thread:"<<endl;
switch(thread)
{
case YES:
cout<<"yes"<<endl;
break;
case NO:
cout<<"no"<<endl;
break;
default:
cout<<"information error!"<<endl;
}
}
int main(void)
{
Cpu c(2000, HIGH, FOUR, NO);
c.show();
cout<<"the size of the class is:"<<sizeof(Cpu)<<endl;
return 0;
}
调试出来是switch语句有问题,没有执行case,直接执行default,而且sizeof的结果觉得应该是2.