|
OnReceive 이벤트의 타입은
TOnReceive 이고요 정의는 다음과 같습니다.
type
TOnReceive = procedure(Sender: TComponent; NumberBytes: Integer; FromIP: string; Port: integer) of object;
C++로 얘기 하자면
typedef void __fastcall (__closeure *TOnReceive)(TObject *Sender, int NumberBytes, AnsiString FromIP, int Port);
대략 이런식인데요
__closeure라는 정의 부분은 원래 C++에는 없던 부분이 BCB에서 추가 된 부분 입니다.
헬프에서 찾아 보면요
The __closure keyword is used to declare a special type of pointer to a member function. In standard C++, the only way to get a pointer to a member function is to use the fully qualified member name, as shown in the following example:
class base
{
public:
void func(int x) { };
};
typedef void (base::* pBaseMember)(int);
int main(int argc, char* argv[])
{
base baseObject;
pBaseMember m = &base::func; // Get pointer to member 'func'
// Call 'func' through the pointer to member
(baseObject.*m)(17);
return 0;
}
However, you cannot assign a pointer to a member of a derived class to a pointer to a member of a base class. This rule (called contravariance) is illustrated in the following example:
class derived: public base
{
public:
void new_func(int i) { };
};
int main(int argc, char* argv[])
{
derived derivedObject;
pBaseMember m = &derived::new_func; // ILLEGAL
return 0;
}
The __closure keyword extension allows you to skirt this limitation, and more. Using a closure, you can get a pointer to member function for an object (i.e. a particular instance of a class). The object can be any object, regardless of its inheritance hierarchy. The object뭩 this pointer is automatically used when calling the member function through the closure. The following example shows how to declare and use a closure. The base and derived classes provided earlier are assumed to be defined.
int main(int argc, char* argv[])
{
derived derivedObject;
void (__closure *derivedClosure)(int);
derivedClosure = derivedObject.new_func; // Get a pointer to the 'new_func' member.
// Note the closure is associated with the
// particular object, 'derivedObject'.
derivedClosure(3); // Call 'new_func' through the closure.
return 0;
}
Closures also work with pointers to objects, as illustrated in this example:
void func1(base *pObj)
{
// A closure taking an int argument and returning void.
void ( __closure *myClosure )(int);
// Initialize the closure.
myClosure = pObj->func;
// Use the closure to call the member function.
myClosure(1);
return;
}
int main(int argc, char* argv[])
{
derived derivedObject;
void (__closure *derivedClosure)(int);
derivedClosure = derivedObject.new_func; // Same as before...
derivedClosure(3);
// We can use pointers to initialize a closure, too.
// We can also get a pointer to the 'func' member function
// in the base class.
func1(&derivedObject);
return 0;
}
Notice that we are passing a pointer to an instance of the derived class, and we are using it to get a pointer to a member function in the base class - something standard C++ does not allow us to do.
Closures are a key part of the C++ Builder RAD environment. They give us the ability to assign an event handler in the Object Inspector. For example, a TButton object has an event called OnClick. In the TButton class, the OnClick event is a property that uses the __closure keyword extension in its declaration. The __closure keyword allows us to assign a member function of another class (typically a member function in a TForm object) to the property. When you place a TButton object on a form, and then create a handler for the button뭩 OnClick event, C++ Builder creates a member function in the button뭩 TForm parent, and assigns that member function to the OnClick event of TButton. This way, the event handler is associated with that particular instance of TButton, and no other.
For more information about events and closures, see Creating events
저도 뭐라는지 잘 모르곘지만,,, ^^
Closeure 라는 지시어를 통해 이벤트를 정의 하고,
그렇게 정의된 이벤트는 TComponent 클래스를 상속 받은 컴포넌트(폼도 컴포넌트죠? ^^)의
멤버 함수로만 할당(연결이 가능하다?)이 가능합니다.
그래서 이벤트는 전부다 TForm이나 TFrame의 멤버 함수로 연동이 됩니다.
^^ 무식이 들통 나기전에 튀어야 겠네요 ㅋㅋ ^^//
자갸 님이 쓰신 글 :
: FAST net Tap에 UDP Socket를 가지고 작업하다가.. 문의합니다.
:
: ((아래 소스는 이곳에서 임의로 작성하다 보니 문법이 틀리거나 또는 델파이 문법과 혼용될 수 있으니
: 개의치 마시고 질문의 중점만 보고 답해 주셔요))
:
:
: TNMUDP mSocket;
:
: Form1::OnReceive(.........)
: begin
:
:
: end;
:
: procedure Onreceive2(.........)
: begin
:
:
: end;
:
: 이 것은 가능하고 정상 작동 되는데..
: mSocket.OnDataReceive = Form1->Onreceive;
:
: 이 것은 왜 안되는 것 일까요?
: mSocket.OnDataReceive = Onreceive2;
:
: 반드시 함수가 폼 클래스 안에서 생성되어야 대입이 가능한데..
: 이거 어떻게 하는지 아시는 분 답변좀 주셔요..
:
: 폼을 안 만들고 소켓 오픈하고 읽고 싶거든요.
|