Objective C ++ Study Notes (1-6)

Source: Internet
Author: User

Guidance:

Keyword explicit

Explicit only applies to constructors and is used to suppress implicit conversions. For example:
Class {
A (int );
};
Int function (a );
When function (2) is called, type 2 is implicitly converted to type. This situation is often not the result that programmers want. To avoid it, you can write it like this:

Class {
Explicit A (int );
};
Int function (a );
In this way, when function (2) is called, the compiler will give an error message (unless the function has an overloaded form that uses int as the parameter ), this avoids errors without the programmer's knowledge.

Summary: explicit only applies to constructors and is used to suppress implicit conversions.

1. Regard C ++ as a Language Federation

(1) c

(2) object-oriented C ++ (C with classes)

(3) template C ++

(4) STL

Where is C ++ more powerful than C? (1) c ++ is compatible with C, that is, C ++ has all c ++. (2) introduces a class, that is, object-oriented. (3) Generic programming. (4) The template library improves programming efficiency.

2. Use const, Enum, and inline whenever possible to replace # define

1) macros are a simple alternative to code without any verification, while inline functions insert Code directly into the call, reducing the resource consumption during normal function calls.
(2) The macro is not a function, but replaces the string in the program with the macro body in the pre-processing phase before compilation.
(3) inline is a function, but it does not generate code independently during compilation. Instead, it embeds the relevant code into the call.
Summary:
For general constants, it is best to replace # define with const and enum;
For macros similar to functions, it is best to replace them with inline functions # define

3. Try to use const

(1) declaring something as const can help the compiler detect incorrect usage;

(2 )...

4. Check whether the object is initialized before it is used.

(1) it is best to initialize the built-in type before use to avoid unnecessary errors;

(2) it is best to use the initial value column instead of using the value assignment operation in the constructor itself.

CA: Ca (int A, float B, string C): M_a (A), m_ B (B), M_c (c) {}// Initial Value column Initialization

CA: Ca (int A, float B, string C): M_a = A, m_ B = B, M_c = c {} // value assignment initialization (the initial value will still be called first, low Efficiency)

5. Understand which functions are compiled and called in C ++

(1) No parameter constructor, but the system does not provide it once the constructor is defined;

(2) copy constructor, such as C C1; C C2 (C1 );

(3) Copy assignment operator
"=", Such as C2 = C1;

(4) destructor

6. If you do not want to use a function automatically generated by the compiler, how can you explicitly reject it.

If each object is unique, the copy construction and assignment between objects are not allowed.

(1) Declare the copy constructor and copy assignment as private.

(2) Declare the copy constructor and copy assignment as private in the base class. The derived class can inherit the base class.


PS: We warmly welcome comments from friends and give some advice on the shortcomings.



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.