Operator Overloading in MFC

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.