More Exceptional C ++ Chinese version test (unusual security issues and technologies)

zhaozj2021-02-11  226

[Herb SUTTER

Name

More Exceptional C

The Chinese version is coming soon. As a translator of this book, I am very happy to recommend this book to everyone. Judging from Huazhong University of Science and Technology Press agreed, I will disclose some translation, please ask everyone to criticize.

]

Abnormal security issues and technology

In modern C programming, there is no knowledge of exception security (Exception Safety), but you want to write a strong code, this is not distant in lovers. indeed so.

If you are using the C standard library, even if you just use New, you have to prepare for an exception. This chapter is based on the corresponding chapter of Exceptional C and discusses new issues and related technologies, for example: std :: uncaumpht_exception () What? Can it help you write more robust code? Does abnormal security affect the design of the class? Or, can it be used as a afterward improvement? Why use the Manager Objects to encapsulate the resources ownership? Why is "resource acquisition is initialization" understanding of writing security code?

But first, let's warm up, look at the basic topic about unusual security; this topic explains the following very important C basic concepts, and analyzes its deeper level: What is the meaning of construct? What is the life of the object?

Terms 17: The constructor failed, one: object life

Difficulty: 4 What happens to the constructor when the constructor produces an abnormality? What happens if an exception is from a sub-object or a member object?

1. Please see the following class:

// Case 17-1 // Class C: Private A {B_;}; How to capture how to capture the constructor from the base class sub-object (eg, a) or member object (e.g., B_) Abnormal?

2. Please see the code below:

// Example 17-2 // {Parrot P;

When is this object of life? When is it over? In addition to the life of the object, what state is the object? Finally, if its constructor throws an exception, what will mean?

answer

After increasing this feature of the Function Try Block in C , the function can capture the range of exceptions slightly. The contents of this Territory are involved:

· In C , the meaning of the object structure and the configuration failure; • When the constructor of the base class or the member object throws an exception, the Function Try Block can be used to transform (rather than suppression) this exception.

Convenience, unless otherwise stated that "members" in this Territor refer to "non-static class data members".

Function Try Block

1. Please see the following class:

// Example 17-1

//

Class C: Private a

{

B b_;

}

In the constructor of C, how to capture an exception thrown from a base class sub-object (eg, a) or member object (eg, b_)?

This is the land of Function Try Block:

/////////Cut: a () Try: a (/ *.) // Optional initialization list, b_ (/ * .. * /) {} Catch (...) {// Once A:: A () or B:: B () We will come here // If A:: A () is successful, then B :: b () Throw an exception, // C language will guarantee that A :: ~ a () will be called before reaching this catch block // to destroy the base class A sub-object that has been created //. }

However, more interesting questions is: Why do you think so? This issue leads to the first one of these terms.

Object Life and Constructor Abnormal Meaning

After a while, we will answer a problem, that is, the constructor of the above C can (or should) absorb the abnormality generated by the constructor of the ABSORB A or B, so that no exception is issued completely. Before you can answer this question correctly, we need to fully understand the concept of the object life 1, and the meaning of the constructor throw an exception.

2. Please see the code below:

// Example 17-2

//

{

Parrot P;

}

When is this object of life? When is it over? In addition to the life of the object, what state is the object? Finally, if its constructor throws an exception, what will mean?

Let us answer a question once:

Q: When is an object's life? A: When its constructor successfully executed and returned normally. That is, when the control (Control) arrives at the end of the constructor, or when an earlier RETURN statement is performed.

Q: When is an object's life? __________________ 1. For simplicity, I am just a type of Class, which is a class of objects, and the life of the structure. A: When its destructor is executed. That is, when controlling the beginning of the enrichment system function.

Q: What state is the object after the end of the object of the object? A: Our answer is as a well-known software master, when talking about a similar code, he intends to "Local Object) is called" he ":

// Example 17-3

//

{

PARROT & Perch = Parrot ();

}

// <- Dioxue starts from here

He is not getting thin! He has passed away! This Parrot is already unfold! He has stopped life! He has died, see his creator! He is a dead body! Be deprived of life, quiet and sleep! If you don't put him on the branch (Perch), it has been restrivet in the loess! [Even earlier, before this code block tail] His metabolic process has been ancient! He left the branch! He has sproked his man, got rid of the troubles of the world, pulled up the curtain of life, joined the singing class, no trace! This is an ex-Parrot!

--Dr. M.python, b.math, ma.sc., ph.d. (compsci) 2 开 玩, the focus here is that after the end of the life period, the state of the object Exactly the same - there is no object exists. That's it. This conclusion takes us to the second important issue:

Q: What does it mean from the constructor? A: This means that the structure has failed, the object never exists, and its life is never started. Indeed, the reporting structure failed - that is, the only way to correctly construct some type of effective object is to throw an exception. (Yes, there is a program rule that has been outdated today: "If the program has a problem, you can set a status flag to 'Bad', so that the caller will check it through an ISOK () function"; , I will talk about my opinion.)

By the way, if the constructor is unsuccessful, the destructor will never be called, which is here - there is no thing to destroy. It can't die, because it has never survived. Please note that this is actually contradictory in this sentence, "an abnormal constructor's constructor. Such a thing is even referred to as an Ex-Object. It has never survived, never added to the object family. It is a non-object.

__________________ 2. Apologize to Monty Python. For the C constructor model, we can summarize as follows: only one of them:

a) The constructor returns normally, ie, controls the tail of the arrival function body, or executes a Return statement. In this case, the object is true. b) After the constructor throws an abnormality. In this case, the object not only does not continue to exist, but in fact it has never been used as an object.

There is no other possibility. With these knowledge, we can better deal with the problems in the next paragraph: Can you absorb an abnormal?

转载请注明原文地址:https://www.9cbs.com/read-4411.html

New Post(0)