class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
}
以上代碼中:
1、enum { IDD = IDD_ABOUTBOX } 這句話什么作用?注:IDD_ABOUTBOX 是一表單的資源ID
2、 //{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
這個
//{{AFX_DATA(CAboutDlg)
//}}AFX_DATA
又起什么作用?
uj5u.com熱心網友回復:
//{{AFX_DATA(CAboutDlg)//}}AFX_DATA
這些是MFC管理的代碼,你最好不要自己改
enum { IDD = IDD_ABOUTBOX }
這是列舉對話框的資源ID
uj5u.com熱心網友回復:
反正是about dialogue的,放那里就好了。。。就是說明各種資源的唄。。。uj5u.com熱心網友回復:
你可以看你的工程資源中的對話框ID,它對應的ABOUT對話框uj5u.com熱心網友回復:
非常感謝,
(1)請問列舉有什么用?
(2)為什么 enum { IDD = IDD_ABOUTBOX } 不直接寫成 IDD = IDD_ABOUTBOX 不就賦個值 嗎?
uj5u.com熱心網友回復:
1:列舉簡單說就是一堆已經命名好的常量。你寫一堆const int x其實也是同樣的效果,但是列舉看起來代碼簡單方便點不是么?
2:你非要標新立異的話有很多方法:
比如.h檔案類里面:static const int iIDD = IDD_ABOUTBOX;
.cpp里面相對應的更改為:CDialog(iIDD,pParent);
或者你弄個全域變數 int g_iIDD = IDD_ABOUTBOX; CDialog(g_iIDD,pParent);都行,絕對的,我可以打包票!
關鍵是為什么要這么干?不過就是定義一個常量IDD_ABOUTBOX而已,IDD_ABOUTBOX就是一個int的常量值!其他屁都不是!微軟寫程式也得遵循C++語法來對不!
uj5u.com熱心網友回復:
從功能上講,列舉和#define是一樣的但是列舉方便,每個名字所代表的值都是自動遞加的
uj5u.com熱心網友回復:
比如#define STUDENT_0 0
#define STUDENT_1 1
#define STUDENT_2 2
#define STUDENT_3 3
#define STUDENT_4 4
#define STUDENT_5 5
很麻煩的
用enum就很簡單
enum{STUDNET_0,STUDNET_1,STUDNET_2,STUDNET_3,STUDNET_4,STUDNET_5,};
這6個元素的值會自動被賦值0,1,2,3,4,5
求給分,我分不夠用了...
uj5u.com熱心網友回復:
正是因為 MFC 不支持這樣寫(.h):static const int IDD = IDD_ABOUTBOX;
所以才 用 enum 的
uj5u.com熱心網友回復:
請問以下代碼 的作用
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
是將視窗資源 指定給 類 CAboutDlg 嗎
uj5u.com熱心網友回復:
Thinking in C++, 2nd ed. Volume 1?2000 by Bruce Eckel
8: Constants
"The “enum hack” in old code
In older versions of C++, static const was not supported inside classes. This meant that const was useless for constant expressions inside classes. However, people still wanted to do this so a typical solution (usually referred to as the “enum hack”) was to use an untagged enum with no instances. An enumeration must have all its values established at compile time, it’s local to the class, and its values are available for constant expressions. Thus, you will commonly see:
//: C08:EnumHack.cpp
#include <iostream>
using namespace std;
class Bunch {
enum { size = 1000 };
int i[size];
};
int main() {
cout << "sizeof(Bunch) = " << sizeof(Bunch)
<< ", sizeof(i[1000]) = "
<< sizeof(int[1000]) << endl;
} ///:~
The use of enum here is guaranteed to occupy no storage in the object, and the enumerators are all evaluated at compile time. You can also explicitly establish the values of the enumerators:
enum { one = 1, two = 2, three };
With integral enum types, the compiler will continue counting from the last value, so the enumerator three will get the value 3.
In the StringStack.cpp example above, the line:
static const int size = 100;
would be instead:
enum { size = 100 };
Although you’ll often see the enum technique in legacy code, the static const feature was added to the language to solve just this problem. However, there is no overwhelming reason that you must choose static const over the enum hack, and in this book the enum hack is used because it is supported by more compilers at the time this book was written.
"
uj5u.com熱心網友回復:
理解成簡單的類和資源對話框關聯就可以了,原因就是樓上說的uj5u.com熱心網友回復:
8樓說的對。。。10樓給出了詳細解答。。。其實樓主也可以去看一下《effective c++》的條款二uj5u.com熱心網友回復:
IDD在創建的時候,被傳入基類的建構式,你不需要去關心CxIocpDlg::CxIocpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CxIocpDlg::IDD, pParent)//在構造的時候傳入基類
{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/93951.html
標籤:基礎類
上一篇:vs2013MFC初學者,撰寫視頻播放器時遇到了點問題,求解
下一篇:輸入一個矩陣生成一個相關的圖
