我有一個std::string
對象已經在程序的視圖類中聲明。如何在mfc應用程序的開始初始化一個變量
//puzzleView.h
public:
std::string currentState; // stores the current state of the blocks
我想就在執行的開始初始化爲一個特定的值。但我在哪裏放置初始化?
我有一個std::string
對象已經在程序的視圖類中聲明。如何在mfc應用程序的開始初始化一個變量
//puzzleView.h
public:
std::string currentState; // stores the current state of the blocks
我想就在執行的開始初始化爲一個特定的值。但我在哪裏放置初始化?
取決於你認爲什麼是「開始執行」。如果您將此字符串聲明爲主視圖的數據成員,則應在視圖類構造函數中初始化它 - 在CPuzzleView::CPuzzleView()
函數體(我認爲您的視圖類名稱爲CPuzzleView
)。這是最常見的情況:
// #1 Using initialization list
CPuzzleView::CPuzzleView(): currentState("No state")
{
}
// #2 Using assignment in ctor body. Also valid, but case #1 is preferable
CPuzzleView::CPuzzleView()
{
currentState = "No state";
}
如果你需要它來進行初始化啓動main()
功能之前,你應宣佈它static
,在任何.cpp
文件的全球範圍內,例如初始化puzzleView.cpp
。但是,不要以爲你真的需要這樣的教育目的(?)任務。
也想提一提,這將是更一致的使用MFC CString
類,而不是std::string
在MFC/ATL應用程序 - 不要混合使用不同的字符串類型,避免不必要的轉換。
您必須初始化.cpp文件中的字符串。
在構造函數中執行 – Jeeva
「執行開始」是什麼意思?在視圖的構造函數中分配值是否爲您的目的發生得太晚? –