More effective C ++ Chapter 1 Reading Notes: pointer, reference, C ++ type conversion, polymorphism array, default constructor

Source: Internet
Author: User

This is a very good book. We recommend you read it. Of course, the book is not necessarily true. Readers need to think and understand it on their own.

 

This article records some of my views.

 

Clause 1: differentiate pointers and references.

 

Of course, this is an old topic. The authors discuss the applicability of the two in terms of semantics. Since the Declaration must be initialized, and no other objects can be referenced after initialization, the pointer declaration does not need to be initialized. After initialization, the pointer can still point to another object.

There is also an example in the book:

Char * Pc = 0;

Char & rc = * PC;

I personally think this is not a good example. This is because it refers to the resolution of a null pointer, and this example does not illustrate any problems. In the latest vs2010, the compiler will give a warning. (Not a lower version of)

A better example is

Char * P = NULL;

Char * & rc = P;

The declared RC references p. Here, RC references a null pointer. At this time, the compiler has no warning. Of course, this is a very strange example. There are many strange things in C ++. This requires everyone to be very careful. Pointer Reference is actually useless. I remember that when I was looking for a job after graduation, the interviewer gave me two quote statements:

Char * & C and char & * C. Which one is correct. Sorry, I was not clear at that time, so I was undoubtedly despised. Fortunately, I didn't go, and the people who went to qunshuo in the next class could not be exploited in it. They had to change jobs in less than two years. Qunshuo is not a good company. Hope that competent friends can think twice before they go.

 

Clause 2: The C ++ style type conversion is preferred.

 

The objective of C ++ type conversion is to improve the type conversion of C language. Therefore, C ++ creates four types of conversion keywords: static_cast, const_cast, dynamic_cast, and reinterpret_cast. Generally, their syntax is static_cast <type> (expresison ).

Static_cast and C basically have the same semantics. It is generally used for static conversion.

Const_cast is used to remove the constant attribute of a constant. In fact, it is more subtle that this is generally used to perform type conversion when passing parameters and temporarily remove constant attributes. In fact, const_cast cannot ultimately change the const attribute of the original object. For example, for the following code:

 

Const int A = 20;

Int & J = const_cast <Int &> ();

J = 30;

Cout <A <Endl;

Cout <j <Endl;

The compiler outputs 20, 30.

Moreover, if you do not need const_cast, a simple C-type conversion can achieve the same effect. If you don't believe it, try it.

So I think that for static_cast and const_cast, to put it bluntly, it is just the name packaging for the C style conversion. In fact, it has not changed much. The same applies to reinterpret_cast.

Macro definition in a book:

# Define static_cast (type), (expression) (type) (expression ))

# Define const_cast (type), (expression) (type) (expression ))

# Define reinterpret_cast (type), (expression) (type) (expression ))

Functions are similar. In some books, C ++ programmers do not need macro definitions. I personally think this is boring. Macro definition is one of the skills that every C/C ++ programmer must possess !!

 

The only exception is dynamic_cast. The macro definition cannot judge the failure, but dynamic_cast can. If the conversion fails, dynamic_cast returns NULL. If you want to return an exception instead of null, You can encapsulate it as follows:

Template <class target, class source>

Inline target * my_cast (source * s)

{

Target * temp = dynamic_cast <target *> (s );

If (temp = NULL)

Throw STD: bad_cast ();

Return temp;

}

This is exactly the implementation of Boost: polymorphic_cast.

Similarly, you can implement static_cast as follows:

Template <class target, class source>

Inline target my_static_cast (source S)

{

Return (target) source;

)

 

I only want to change the type of C ++. If you have better suggestions, contact me. Or post a comment.

 

Clause 3: Do not apply polymorphism to arrays.

I personally think this clause is a design issue. You can skip this step.

 

Clause 4: Avoid unnecessary Default constructors.

 

The default constructor of C ++ is clearly described in the book "Inside C ++ object model. When you do not declare any constructor, the compiler will help you implicitly declare a default constructor. It should be noted that the default constructor usually refers to the constructor without parameters. In some cases, the compiler will help you generate a non-trivial constructor. For example, your class contains another complex type, or in the inheritance system, when a virtual function is declared, the non-trivial constructor generated by the compiler calls the default constructor of these types. The default constructor of the base class, initialize virtual function tables. In other cases, the compiler considers the default constructor to be trivial. In this case, the compiler does not generate default constructor for you.

 

I am still a little interested in the content of this chapter. In terms of design and language features, the default constructor is required in some cases, but sometimes the default constructor is not required. Therefore, you need to weigh this. However, I personally think it is a good habit to provide the default constructor and initialize class resources and variables in the default constructor. Of course, this is my personal opinion.

 

In addition, there is an interesting example in the book: How to Use the constructor of the USER parameter to initialize an object array:

Class

{

Int I;

Public:

A (int I _): I (I _){}

};

 

Void * rawmemory = Operator new [] (10 * sizeof ());

A * array_of_a = (A *) (rawmemory )'

For (INT I = 0; I <10; I ++)

New (array_of_a + I) a (I );

 

Delete after use:

 

For (INT I = 0; I <10; I ++)

Array_of_a [I]-> ~ A ();

Operator Delete [] rawmemory; the Allocator Implementation of STL also uses the above similar practices. For C ++ operator new, operator Delete, placement new, and placement Delete, This is not suitable for beginners. It is so tough. Anyway, it is a not book for beginners. Now, let's write it first. Next we will have Reading Notes for Chapter 2. I hope I can read this book quickly.

 

 

 

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.