Objective C ++ Reading Notes (0-4)

Source: Internet
Author: User

I read this book a year ago, and I am confused about C ++. Now I am officially engaged in coding. I have read other people's code and found that I don't actually know much about it. So I picked up my previous book and started to take notes.

0 -- Preface

Four transformation operators: static_cast <int> (32.12)
Const_cast: transforms the constant type of an object or pointer.
Dynamic_cast: used to perform "Safe downward transformation"
Reinterpret_cast: the transformation result depends on the compiler.
Static_cast: no other suitable conversion operators are available. Use this method, which is the closest to the traditional transformation operators.

Item 1 -- replace const and inline as much as possible # define

The advantage of using const is that you can directly obtain variables rather than defined numbers during debugging. This is useful when using GDB to trace code, such as # define num 123; if print num in GDB, there will be a problem where num cannot find the symbol table. In this case, when num is used for watch in a complex expression, it is very depressing to go back to the specific value of num.

Not only is const used, but the semantics that cannot be modified is the essence of its existence,
Constant pointers and pointer constants are easy to mix and record. The asterisks are in the middle and the left fixed content is right fixed.
Const char * P; char const * P indicates that the content pointed to by P cannot be changed.
Char * const P indicates that the pointer cannot be changed
Another thing about BT is that typedef is involved. typedef can destroy the const semantics. refer to the following link.
Http://hi.baidu.com/%C6% AE %D4%DA%B1%B1%BE%A9/blog/item/92e3554e22307d0db2de0540.html
Http://hi.baidu.com/xiaoshumuzi/blog/item/34ff22386487eaf8b311c716.html

Abandon # The reason why define uses inline is similar to # define max (A, B) (a)> (B )? (A) (B) in addition to using countless bits, the simple expression max (++ a, B) can easily discard the programmer's intention, this is enough to give up # define

Item 2 -- replace <stdio. h> with <iostrem>

EC (usually tive C ++) provides the item2 reason. The CIN/cout type is safe, such as rational R; cout <R is unimaginable in stdio. h. Of course, rational should support <, and write operator <is another item to be discussed. The correct statement is provided here.
Class rational {
Friend ostream & operator <(ostream & S, const rational & R)
}

As a matter of fact, for those who use CIN/cout when learning this kind of thing, I feel like stdio. h, which is very fun, involves underlying I/O interfaces such as fopen, fput, and fwrite. in-depth research is also a better understanding of computer I/O.

Item 3 -- replace malloc and free with new and delete as much as possible

New process: Apply for memory (that is, the role of malloc), call the constructor, and return the Object Pointer (the operator new and placement new are based on this basic knowledge)

In addition, it is common sense that new and delete correspond to malloc and free, but why? As mentioned in EC, mixed use may lead to unexpected errors.

Item 4 -- try to use C ++-style annotations

In fact, it is relative/**/, which is widely used for this annotation. // In vs2005 and eclipse, there are shortcut keys. vs2005 is Ctrl + k, C (press Ctrl, press K and c) to cancel Ctrl + k, U, eclipse is much more convenient, just use Ctrl +/

Speaking of comments, we can't help but mention doxygen. Recently, I am also reading the source code, so I have to study the annotation standards. For simple search, follow the link below:
Http://blog.163.com/jallyx/blog/static/5726580020097611042993/
Http://zsxxsz.javaeye.com/blog/403539
Http://www.lupaworld.com/action_viewstutorial_itemid_3735.html

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.