Create a "dialog box-based" MFC program and double-click "OK" to add a message response function to it. The Code is as follows:
Void ctestdlg: onbnclickedok ()
{
Crect rect;
Getclientrect (rect );
Cstring STR;
Str. Format (_ T ("width = % d, Height = % d"), rect. Width (), rect. Height ());
MessageBox (STR );
}
I have a problem with this Code. Have you ever thought about it? Getclientrect is a member function inherited by ctestdlg from cwnd. Its prototype is
Void getclientrect (lprect) const;
The input parameter is a pointer to the crect object, but we pass in the crect object. The compilation is correct without any errors and the program runs properly.
In fact, this is not accidental. This is because the crect class reloads the lprect operator, that is, if the lprect is needed, we pass
Is crect, the compiler will call the lprect operator function to convert crect to lprect (of course, this conversion [or operator overload] is Microsoft
Compiled for us. If there is no such overload compiler, an error will be reported because the type does not match ).
The following is an example written by myself. Let's think about why?
Class mypoint;
Typedef mypoint * lpmypoint;
Class mypoint
{
Public:
Void output ()
{
Afxmessagebox (_ T ("hello "));
}
Operator lpmypoint ()
{
Return this;
}
};
Void showmessage (lpmypoint)
{
Lpmypoint-> output ();
}
Void ctestdlg: onbnclickedok ()
{
Mypoint;
Showmessage (mypoint );
}