更新时间:2023-02-12 12:16:47
首先使用double作为类型,因为它避免了混淆,在不同的语言中更精确和相同。
这些代表你可以设想一个变量,它们使用函数指针作为值。
设计的一些提示: b $ bpublic delegate void OnStkQuote_Charp( double newPrice, double openPrice);
你真正需要的是什么。不多,但更少。
如果我理解为股市用例,你***有2个功能:
public delegate void OnStkQuoteOpen_Charp( double openPrice); // 只有一个会话开盘价
public delegate void OnStkQuoteUpdate_Charp( double newPrice); // 永久更新
您应该从这篇文章。
Some sample code
namespace CppWrapper {
public delegate void OnStkQuote_Charp(double dPrice);
public delegate void OnDataOut(v_struct pData);
public ref class v_struct
{
public:
float New;
float Open;
};
public ref class CppWrapperClass
{
public:
CppWrapperClass();
static void OnStkQuot(struct STKDATA *pData) <-- loop callback
{
v_struct^ vStruct = gcnew v_struct();
vStruct->New = pData->New;
vStruct->Open = pData->Open;
OnStkQuote_Charp^ tmp1 = _Event;
if (tmp1) {
tmp1->Invoke(pData->New);
}
OnDataOut^ tmp = _DataOut;
if(tmp)
{
tmp->Invoke(vStruct);
}
};
event OnStkQuote_Charp^ OnQuote_Charp
{
void add(OnStkQuote_Charp ^ d) {
_Event += d;
}
void remove(OnStkQuote_Charp ^ d) {
_Event -= d;
}
void raise(double dPrice) {
OnStkQuote_Charp^ tmp = _Event;
if (tmp) {
tmp->Invoke(dPrice);
}
}
}
event OnDataOut^ OnData_Charp
{
void add(OnDataOut ^ d) {
_DataOut += d;
}
void remove(OnDataOut ^ d) {
_DataOut -= d;
}
void raise(v_struct pData) {
OnDataOut^ tmp = _DataOut;
if (tmp) {
tmp->Invoke(pData);
}
}
}
private:
static OnDataOut ^ _DataOut;
static OnStkQuote_Charp ^ _Event;
};
}
XXX.h
C++ struct
struct STKDATA
{
float New;
float Open;
}
What I have tried:
I want to event out struct,i cant event out double/int/string but i don't know how to delegate struct ,maybe this code is wrong .... How can i modifiy this code ? please help Thanks~
At first use double as type, because it avoides confuses, is more precise and the same in the different languages.
These delegates you can imagine a variables which are using pointers to functions as as values.
Some tips for the design:public delegate void OnStkQuote_Charp(double newPrice, double openPrice);
Is what you really need. Not more, but even less.
if I understand it right as stock market use case, you better served with 2 functions:public delegate void OnStkQuoteOpen_Charp(double openPrice);//only one opening price a session public delegate void OnStkQuoteUpdate_Charp(double newPrice);//permanent updates
A detailed example of such implementation you should fetch from this article.