C/C++使用心得:enum与int的相互转换

23 篇文章 22 订阅

如何正确理解enum类型?


例如:

enum Color { red, white, blue}; 
Color x;


我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将其理解成int类型。

 

我们再看enumeration类型:

enum Color { red, white, blue};


(C程序员尤其要注意!)
理解此类型的最好的方法是将这个类型的值看成是red, white和blue,而不是简单将看成int值。
C++编译器提供了Color到int类型的转换,上面的red, white和blue的值即为0,1,2,但是,你不应简单将blue看成是2。blue是Color类型的,可以自动转换成2,但对于C++编译器来说,并不存在int到Color的自动转换!(C编译则提供了这个转换)

例如以下代码说明了Color会自动转换成int:

enum Color { red, white, blue };
 
void f()
{
   int n;
   n = red;    // change n to 0
   n = white;  // change n to 1
   n = blue;   // change n to 2
} 


以下代码也说明了Color会自动转换成int:

void f()
{
   Color x = red;
   Color y = white;
   Color z = blue;
 
   int n;
   n = x;   // change n to 0
   n = y;   // change n to 1
   n = z;   // change n to 2
} 


但是,C++编译器并不提供从int转换成Color的自动转换:

void f()
{
   Color x;
   x = blue;  // change x to blue
   x = 2;     // compile-time error: can't convert int to Color
} 

 

若你真的要从int转换成Color,应提供强制类型转换:

void f()
{
   Color x;
   x = red;      // change x to red
   x = Color(1); // change x to white
   x = Color(2); // change x to blue
   x = 2;        // compile-time error: can't convert int to Color
} 


但你应保证从int转换来的Color类型有意义。
 

参考链接: 点击打开链接
  • 21
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
QUENCY_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; CFFT m_fft; CTChart1 m_chartTime; CTChart1 m_chartFreq; CComboBox m_comboWindow; CComboBox m_comboFunction; int m_iSampleRate; int m_iFFTLength; int m_iOverlap; double m_dWindowParam; double m_dAmplitude; double m_dTimeStep; double m_dFreqStep; BOOL m_bLogScale; BOOL m_bShowGrid; BOOL m_bShowLegend; BOOL m_bKeepData; BOOL m_bIsProcessing; CEdit m_editSampleRate; CEdit m_editFFTLength; CEdit m_editOverlap; CEdit m_editWindowParam; CEdit m_editAmplitude; CEdit m_editTimeStep; CEdit m_editFreqStep; CButton m_checkLogScale; CButton m_checkShowGrid; CButton m_checkShowLegend; CButton m_checkKeepData; CButton m_buttonStart; CButton m_buttonStop; CString m_strWindowTitle; CString m_strXLabelTime; CString m_strXLabelFreq; CString m_strYLabelTime; CString m_strYLabelFreq; // 生成的消息映射函数 protected: virtual BOOL OnInitDialog(); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: afx_msg void OnBnClickedButtonStart(); afx_msg void OnBnClickedButtonStop(); afx_msg void OnCbnSelchangeComboWindow(); afx_msg void OnCbnSelchangeComboFunction(); afx_msg void OnEnChangeEditSamplerate(); afx_msg void OnEnChangeEditFftlength(); afx_msg void OnEnChangeEditOverlap(); afx_msg void OnEnChangeEditWindowparam(); afx_msg void OnEnChangeEditAmplitude(); afx_msg void OnEnChangeEditTimestep(); afx_msg void OnEnChangeEditFreqstep(); afx_msg void OnBnClickedCheckLogscale(); afx_msg void OnBnClickedCheckShowgrid(); afx_msg void OnBnClickedCheckShowlegend(); afx_msg void OnBnClickedCheckKeepdata(); void InitControls(); void UpdateControls(); void EnableControls(BOOL bEnable); void StartProcessing(); void StopProcessing(); BOOL ProcessData(double* pData, int iDataLength); static UINT DoProcessing(LPVOID pParam); }; 请问,这段代码是用来做什么的?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值