2013-03-25 33 views
1

我想要使用自定義的.NET引用類來控制按鈕系統。它由添加到父窗體的類中的PictureBox組成。當它檢測到點擊時,它需要調用構造函數中指定的函數,該構造函數是父類中的方法。指針到成員函數在參考類

例如:

//in the custom class file 
public ref class CButton { 
    private: void (*callingproc)(void); 
    public: 
    CButton(void (*cproc)(void)) { 
     callingproc = cproc; 
    } 
    button_dowork() { 
     //do our code to detect if the click was in the right place and call our proc 
     callingproc(); 
    } 
}; 
//in the form.h 
void cp(void) { 
    //do our form work 
} 
void Form_CreateCButton() { 
    CButton^ t = gcnew CButtom(cp); 
} 

上面導致沿着線錯誤「使用&納秒::形式:: CP創建指針構件」,其次是「指針到構件無效爲受管理的類「當我按照說明。有任何想法嗎?

+2

您*必須*在此處使用委託。在C++/CLI編程的任何介紹性文章中都有涵蓋。 – 2013-03-26 00:18:15

+0

從C++重新標記爲C++ - CLI – JBentley 2013-03-26 01:31:13

+0

「在C++/CLI編程的任何介紹性文本中都有詳細介紹」 - 任何鏈接到此的機會? – Dave 2015-01-09 12:05:15

回答

0

看起來你的類正在存儲一個指向(全局)函數的指針。也許你的void cp()函數是在一個類(「form」?)內部。在這種情況下,它實際上是一個成員函數。全局函數與成員函數不同,所以類不能存儲指針。

也許你應該考慮代表(尤其是如果你使用.Net)。請參閱: http://msdn.microsoft.com/en-us/library/ms379617(v=vs.80).aspx