More Effective c study notes three: abnormal

1. destructors prevent resource leaks

 

Consider the following:

void processAdoptions(istream& dataSource)
{
    while(dataSource)
    {    
        ALA *pa = readALA(dataSource);
        pa->processAdoption();
        delete pa;
    }
}

If pa-> processAdoption () abnormal operation will not delete resource leaks.

Method a: try catch block wrapped, but in this way multiple duplicate code

Method two: Packaging pointer (pointer intelligent) pointer-like objects in the object is released destructor.

 

Thought: object store with a resource to be automatically released, and then rely on the destructor of the object to release resources. Using this can be a good idea to avoid resource leaks occurred in the presence of an abnormal environment.

 

2. To prevent resource leaks in the constructor

Idem, and packaging smart pointer pointer object, to prevent the problem in the constructor.

class BookEntry{
public:
    ...
private:
    ...
    const auto_ptr<Image> theImage;
    const auto_ptr<Audioclip> theAudioClip;
}

BookEntry::BookEntry(const string& name, const string& address, const string& imageFileName, const string& audioClipFileName)
:theName(name), theAddress(address), theImage(imageFileName != "" ?
                                    new Image(imageFileName : 0),
                                   theAudioClip (audioClipFileName != "" ? new AudioClip(audioCipFIleName) : 0))
{

}

3. prohibit the abnormality information transmitted to the outer destructor

Session::~Session()
{
    try{

    logDestruction(this);
}
castch(...){}


}

Catch all exceptions, so that the thrown exception is not transmitted to the outside destructor session.

effect:

1. In the process can be removed to elucidate the stack of abnormal transfer, being called to prevent terminate

2. It can help ensure that the destructor can always complete and we want it to do all things.

 

4. The difference between understanding "throws an exception" and "pass a parameter" or "a virtual function call"

throw an exception to catch some of the similarities and differences in the function parameter passing.

1. They are by value, passing a pointer or pass by reference. But when you call the function, control program will eventually return to the calling of the function, but an exception is thrown never return to local control of throwing an exception.

2.c ++ specifications must be passed as an object to be copied and an exception is thrown, it is thrown to run slower than the speed of passing parameters.

3. When the exception object is copied, the copy operation is performed by the copying function object, the object is a static class corresponding to the type of copy constructor.

4. exception objects thrown by the capture normal reference, it does not require a reference to a const object by pointing capture, a temporary object is not allowed to pass in a function call to a non-const reference parameter in the type.

5.catch (Widget w) such abnormal transfer create two copies of the object is thrown, a temporary object is that all exceptions that must be established, the second temporary object is copied into the w ( two ) throws pointer can point to local objects, must be a global or heap.

Parameter 6.catch (var value) is generally not transmitted implicit conversion, but conversion can be two types of abnormality matching catch clause A derived class is to convert between the base class, second allows from one type of pointer into untyped pointer, const void * pointer with a catch clause can capture any type of pointer type of exception.

7.catch clause exception type sequence matching the order in which they appear in the source code, the first type will be successfully matched catach to perform.

 

The catch exceptions by reference

Capture by value or by reference (pointer problem).

By value will be a problem, with a similar function (When an object is passed by value to a function, static binding (estimated to be copying type conversion and lost polymorphism))

Avoiding the problem by reference exception capture, capture the pointer will not and can capture standard type of exception because the problem deleted objects. Also not captured by the abnormal value, this method does not slicing problem, and the exception object are copied only once.

Recommended references catch exceptions.

 

6. Use caution exception specifications

c ++ 11 use noexcept 

1. Avoid using exception specifications in the template with a type parameter.

template<class T>
bool operator==(const T& lhs, const T& rhs) throw()
{
    return &lhs == &rhs;
}

Templates and exception specifications do not mix.

 

2. If there is no exception specifications call other functions in a function exception specification should be removed this function.

 

7. Understand the overhead of exception handling

 

 

 

 

 

 

 

 

 

 

 

 

Published 44 original articles · won praise 5 · Views 1398

Guess you like

Origin blog.csdn.net/qq_33776188/article/details/104629039