"Effective C++" (2)

Insert picture description here

Preface

Gathering with the big brothers today, I came into contact with a lot of new things.
I am striving to finish this series today. Recently, the information is a bit too much to analyze. First, send it out slowly.
Please give me some time, and I will wait a moment.

Next, there will be big moves.


13, smart pointer

I won't say much, just use it directly.

18. Make the interface easier to be used correctly and not easy to be misused

I skipped a wave in the middle, no reason, I don't understand.

This clause reminds me of the "template method pattern", but I don't know how to use this pattern, or how to use it, or what it's useful for. Now I know another usage.

Take the first small demo I made, which is an input and output control function.
This demo has also been widely used in my teaching work. Although the answers submitted at the end are not very ideal, let me say that friends who are interested can implement it by themselves:

1、语言不限,最好是C/C++,Python封装的太好了,体现不出我们的水平
2、输入一个字符串
3、我要控制输入的为纯英文
4、我要控制输入的为纯数字
5、我要控制输入的可以有英文、数字、标点混搭
6、基于以上3/4/5,我要控制输入长度一定
7、我输错了要支持回删
8、我要密文输入,不能在屏幕上显示我输入的是什么

This is the template, and wrong input will not be counted.
It's like writing a line on the screen: Please enter English and numbers, and then I can still enter it casually. I enter a bunch of punctuation and tell me that the input is illegal. What's wrong? Illegal you allow me to enter?


22. Declare member variables as private

I always do this.
That's it.


23. Replace member function with non-member and non-friend

I didn't know what was going on before, and I felt that the teacher was such an extra effort, but now I know. This is the template method pattern. In order to protect the packaging.

Assumption:

class A{
    
    
public:
	a();
	b();
	c();
}

To solve a solution, you need to call abc in turn, should it be in the scene class:

A *AA = new A();
AA->a();
AA->b();
AA->c();

?

still is

class A{
    
    
public:
	run(){
    
    
		a();
		b();
		c();
	}
}

int main(){
    
    
	AA->run();
}

What do you think?


26. Delay the appearance of variable definitions as much as possible

Some books or tutorials will tell you that variable definitions are written at the beginning of the function under the guise of programming specifications and programming art. This is the case in the C language. Otherwise, the following variables will not be able to allocate resources, resulting in an error. But C++ is not like that.

You have to be clear, once there is a branch inside the function, some objects may not have the opportunity to appear on the scene at all, so why do you define it so hurriedly?

Once variable definitions are involved, there will be construction and analysis costs, regardless of whether it is used or not.

You should not only postpone the definition of a variable as much as possible, it is best to postpone it until the moment when the variable can be assigned a value.


Of course, the specific situation is analyzed in detail. If you encounter a temporary variable in the loop, it is another matter.

There are no more than two situations in the loop, the definition outside the loop and the definition inside the loop:

11个构造函数+一个析构函数+n个赋值操作
2、n个构造函数+n个析构函数

If the cost of assignment is less than the cost of construction + destruction (the variable is not large), then choose the first method.
At this time, it depends on experience.


27. Try to minimize transformations

If it is your own transformation operation, do less and try not to do it.
Because of different compilers or different platforms, they may not recognize each other.


31. Minimize the compilation dependency between files

If you want to use it, you have to use it. How can this be reduced? We don't want to be full and have nothing to do to quote a header file that is not needed.


Go here

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/115033379