[問題] c++/cli 建構子多載 (ctor overloading)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++ 2008
問題(Question):
請問ref class (Windows Forms, or say Controls)
標準的constructor overloading寫法是甚麼呢?
public ref class form2 : public System::Windows::Forms::Form
{
public:
form2(void) {InitializeComponent();/*and then some base actions*/}
form2(String^ cap)
{
/*我想要call form2()作初始化動作*/
label1->Text = cap;
}
private:
InitializeComponent() {/*VC自動產生區塊 不能動*/}
}
第一種寫法
form2(String^ cap) : form2() {...}
聽說這是C++11的 delegate ctor 不過VC2008編譯不給過 要後面版本才給過
第二種寫法
form2(String^ cap) {
form2();
label1->Text = cap;
}
這好像不是C++允許的寫法 VC可以編譯過 所以應該是c++/cli的專屬語法
然而奇怪的地方是 執行時label1會成為無定義 導致例外狀況
第三種寫法
form2(String^ cap) {
InitializeComponent();
form2();
label1->Text = cap;
}
這樣既能初始化 執行時label1也不會出現例外的狀況
跑起來很順利,可是InitializeComponent()卻要執行兩次 感覺非標準解
還有C#專用語法 和 最後一招 init() 我就不詳列出來了
主要的疑惑在於
既然都允許呼叫form2()了
為何還非要直接呼叫InitializeComponent()?
所以(VC2008以前)最標準寫法還是另設init()或複製貼上大法嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.171.197.246
→
12/23 15:36, , 1F
12/23 15:36, 1F
→
12/23 15:37, , 2F
12/23 15:37, 2F
→
12/25 18:51, , 3F
12/25 18:51, 3F
→
12/25 18:54, , 4F
12/25 18:54, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章