A small defect in C ++ Design -- it is not necessary to force a class common member function to declare in the class body.

Source: Internet
Author: User
Should a defect in the C ++ design be submitted to the C ++ Standards Committee? -- You do not need to force a class common member function to declare it in the class body.
Ordinary member functions of the class must be declared in the class to write member functions outside the class. Why must C ++ be designed like this?
1. If you want to add a function, write the same function header twice, which violates the unique information principle. This causes problems such as difficulty in maintenance and synchronization.
2. It is impossible to add a member function to the class outside the library.
For example, I want to add a custom tostring () function to the cstring class of the MFC library.

I think we should design this way. Common class member functions can be defined externally without being declared in the class.

For example, cstring:
Directly in myProgramWrite in

Mylib. cpp:

Lptstr cstring: tostring ()
{
Return (lptstr) (lpctstr) * this;
}

In this way, you can add a tostring member function for the cstring class in the MFC library.

--------------------------
Usemylib. cpp
...
{
.....
Lptstr cstring: tostring (); // you only need to declare it before using it. Of course, you can declare it in a header file.
STR = CSTR. tostring ();
...
}

This problem has been boring for nearly 10 years.
Of course, something like a virtual function must still be declared in the class body.

However, I think BS should have considered this issue for a long time. Why did he use the current solution?

Related Article

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.