Rereading Objective C ++

Source: Internet
Author: User

1:
Constants can be placed in header files, including constant strings, but constant strings should be defined as follows:
Const char * const pstr = "str ";
Const char STR [] = "str ";

 

2:

If the memory is insufficient, new runs out of a bad_alloc exception. If a handler function with insufficient memory is set, this function is either

Exit abort or find available memory! Otherwise it will be in an endless loop!

However, when operator new is called to allocate an airplane object, the resulting memory may be larger

You need more. This strange behavior occurs because operator new and operator Delete need to interact with each other.

Transfer information.
Therefore, we don't really care too much about the new failure issue, or capture the bad_alloc exception to let the program

3
Rewrite the copy constructor and value assignment constructor for classes that require dynamic memory

Do not return the internal data handle of the class. If you have to return the data, the const handle is returned.
Eg string: const char * operate ();
Or string: const char * c_str ();

////////////////////////////////
2009.9.9
1: Do not redefine the inherited non-virtual functions
2: Avoid downward conversion. You can try to implement it using virtual functions (downward conversion is generally to use a function of the derived class. If virtual functions are used, the function of the derived class is automatically used, can avoid downward conversion). Actually, it cannot. Use dynamic_cast <> Security Point.
3: You do not need to forcibly convert the return value of malloc (). It is better to use sizeof (* P) or sizeof (variable ).
4: How to Prevent the constructor from leaking resources: Try to catch any exceptions such as new. If any exceptions occur, delete all pointers.
The ultimate method is to use auto_prt <> to store pointers. This relies on the C ++ guarantee. Generally, if a class is constructed, its destructor can be called.
5. Accept exception objects by referencing
Pointers face the issue of deleting pointers.
Data transmission faces the cutting problem.

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.