Interpretation of Objective c ++ Series 1 (Entry 1-5)

Source: Internet
Author: User

Preface
Although I have been developing C/C ++ for several years, I feel that my basic aspects are still a little weak. I am not clear about many details. I hope to write a blog to deepen my understanding.

I recently read this book and felt that I had benefited a lot. So I decided to write some articles to introduce my understanding of these articles.

 

Clause 1: Try to use const and inline instead of define.
Macro constant
There are two problems with macro constants: one is no type, and the other is hard to find when an error is reported during compilation, because it also replaces the macro with a constant.
Therefore, using the const constant can avoid the above problems. If it is written in C language, you should use the macro definition.
Macro Functions
In the past, I mainly used C language development. During a natural interview, I would ask questions about macro definition.
One bad question is to use a macro definition to find the two largest numbers.
The answer is:
# Define MAX (a, B) (a)> (B )? (A) (B ))
That is to say, all variables, expressions, and macro functions must be enclosed in brackets. Why?
Brief Description
If you write
# Define MAX (a, B) a> B? A: B
So now I calculate MAX (2, 3) * MAX (2, 2) will be translated
2> 3? 2: 3*1> 2?
In this case, you need to query the priority table ,? : The priority is less than *, so the actual result above is
2> 3? 2 :( 3> 2? )
2> 3? 2:1
The final result is 1.
Instead of what we want.
Even if this is done, there are still problems with the symbols such as ++ and. You can read the examples in the book.
This is the biggest problem with macro definition.
So if you use inline functions instead, you can write them as follows:
[Cpp]
Inline MAX (int a, int B)
{
Return a: B? A> B
}

Inline MAX (int a, int B)
{
Return a: B? A> B
}
However, macro definition itself is characteristic of adapting to various situations. In this case, we need to use templates to solve this problem.


[Cpp]
# Define MAX (a, B) (a)> (B )? (A) (B ))
 
Template <typename T>
Inline const T & max (T & a, T & B)
{
Return a> B? A: B;
}

# Define MAX (a, B) (a)> (B )? (A) (B ))

Template <typename T>
Inline const T & max (T & a, T & B)
{
Return a> B? A: B;
}
The write efficiency is as high as the macro, and it is better to maintain than the Macro. We don't need to add brackets to all the places when memorizing the macro definition, you don't have to fight when calling a macro function. You don't need to use ++, --, or other symbols. Sometimes you need to show it.


The following is a complete program to analyze this problem.
[Cpp]
# Include <iostream>
Using namespace std;
 
# Define MAX (a, B) (a)> (B )? (A) (B ))
 
Template <typename T>
Inline const T & max (T & a, T & B)
{
Return a> B? A: B;
}
 
Int main ()
{
Int a = 4;
Int B = 7;

Cout <MAX (++ a, ++ B) <endl;

A = 4;
B = 7;
Cout <max (++ a, ++ B) <endl;
 
}

# Include <iostream>
Using namespace std;

# Define MAX (a, B) (a)> (B )? (A) (B ))

Template <typename T>
Inline const T & max (T & a, T & B)
{
Return a> B? A: B;
}

Int main ()
{
Int a = 4;
Int B = 7;

Cout <MAX (++ a, ++ B) <endl;
 
A = 4;
B = 7;
Cout <max (++ a, ++ B) <endl;

}
As you can see, the output results are 9 and 8 respectively.

 


Clause 2: <iostream> instead of <stdio. h>
This clause has different opinions.
The statement in the book is that you don't need to remember those types. % d indicates the output types in the decimal book, such as % s, % c, and % f,
You don't have to worry about getting an address in scanf.
For Class output, you can override <or> to define their input and output, but it may be difficult to print with scanf and printf.


However, some people oppose using <and> for input and output. The reason is that the Code is easy to understand.
You can see in scanf and printf that they will format the output and output,
For example, if the input format is input1 and input2 must be separated by commas, iostream may be difficult to write.
In addition, you can clearly read the meaning of this log in printf, but it is not easy to read it in stream representation. This is applicable to scenarios.

 


[Cpp]
# Include <iostream>
# Include <stdio. h>
Using namespace std;
 
 
Int main ()
{
Int a, B;
Float c, d;
Cin> a> c;
Cout <a <"" <c <endl;
Printf ("enter \ n in 1 or 2 format ");
Scanf ("% d, % f", & B, & d );
Printf ("% d, % f", B, d );
}

# Include <iostream>
# Include <stdio. h>
Using namespace std;


Int main ()
{
Int a, B;
Float c, d;
Cin> a> c;
Cout <a <"" <c <endl;
Printf ("enter \ n in 1 or 2 format ");
Scanf ("% d, % f", & B, & d );
Printf ("% d, % f", B, d );
}

 


Clause 3: Try to use new and delete instead of malloc and free
Let's first look at an example.
[Cpp]
Int main ()
{
Int * p;
Int * q;

P = new int [10];
Q = (int *) malloc (sizeof (int) * 10 );
}

Int main ()
{
Int * p;
Int * q;

P = new int [10];
Q = (int *) malloc (sizeof (int) * 10 );
}
After compilation, you will find that the compilation fails. Why? Because new is an operator, it is a symbol like =, +, *, and so on. The Compiler knows them.
But malloc is actually a function, and the compiler does not know them, so you need to include the header file stdlib. h.


What is the difference? That is, the emergence of new and delete is for the class. They will call the class constructor and destructor. However, when applying for malloc and free, the constructor and destructor are not called. Therefore, it is recommended that new and delete replace malloc and free in c ++.


Also, do not mix the four. Be sure to match.
For simplicity, new and delete can be used to implement all functions. Therefore, we recommend using this in c ++. If it is a c language, use malloc and free.


Clause 4: Try to use c ++-style comments
C ++ comments are // and c comments are /**/
Here, I personally think it is best to use the/**/segment annotation for the function header annotation.
If it is a single-line comment, you can consider using //


Clause 5: The new and delete operations must be in the same format.


Another problem is that new [] and delete [] correspond to each other. delete [] should be used for release when new [] is applied.
If you apply for a new application and use delete [] to release the application, it will lead to more releases and unexpected problems.
Similarly, if new [] is applied, and delete is used for release, if it is of the basic type, there is no problem that delete is the same as delete [], but the complex types are different. Let's look at the example below.


[Cpp]
# Include <stdlib. h>
# Include <iostream>
Using namespace std;
 
Class
{
Public:
Int;
};
 
Void testInt ();
Void testClass ();
 
Int main ()
{
TestInt ();
TestClass ();
}

Void testInt ()
{
Int * p;
Int * q;

P = new int [10];
Q = (int *) malloc (sizeof (int) * 10 );

P [1] = 10;
Q [1] = 10;

Delete [] p;
Free (q );

Cout <p [1] <endl;
Cout <q [1] <endl;
}
 
Void testClass ()
{
A * p = new A [10];
P [1]. a = 10;

Delete p;
Cout <p [1]. a <endl;
}

# Include <stdlib. h>
# Include <iostream>
Using namespace std;

Class
{
Public:
Int;
};

Void testInt ();
Void testClass ();

Int main ()
{
TestInt ();
TestClass ();
}
 
Void testInt ()
{
Int * p;
Int * q;

P = new int [10];
Q = (int *) malloc (sizeof (int) * 10 );

P [1] = 10;
Q [1] = 10;

Delete [] p;
Free (q );

Cout <p [1] <endl;
Cout <q [1] <endl;
}

Void testClass ()
{
A * p = new A [10];
P [1]. a = 10;
 
Delete p;
Cout <p [1]. a <endl;
}
The output result is
1629738516
0
10
We can see that the class is not released completely.
Because both malloc and free are specified sizes, you know the size of the allocated space. free only needs to release the corresponding space.
When new is used, there are two conditions: new is an object, or new [] is used to allocate an array of objects.
Therefore, delete must explicitly tell whether it is an object or an array of objects.

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.