C ++ core principles C.33: if the class contains a pointer members ownership defined destructor

C.33: If a class has an owning pointer member, define a destructor

C.33: if the class contains a pointer members ownership defined destructor

 

Reason (reason)

 

An owned object must be deleted upon destruction of the object that owns it.

Dependent objects must be destroyed by the owner class destructor owns the object.

Example (Example)

A pointer member may represent a resource.A T* should not do so, but in older code, that's common. Consider a T* a possible owner and therefore suspect.

Pointer members may be used to express a resource. T * should not do that, but in some old code, this practice is very common. Taking into account the possibility of T * as the owner use and confirm.

template<typename T>
class Smart_ptr {
    T* p;   // BAD: vague about ownership of *p
    // ...
public:
    // ... no user-defined default operations ...
};

void use(Smart_ptr<int> p1)
{
    // error: p2.p leaked (if not nullptr and not owned by some other code)
    auto p2 = p1;
}

Note that if you define a destructor, you must define or delete all default operations:

Note: Once defined destructor, it is necessary to define or disable all default actions.

 

Translator's Note: This is the default action refers to the default constructor, copy / move constructor, copy / move operator and destructor.

template<typename T>
class Smart_ptr2 {
    T* p;   // BAD: vague about ownership of *p
    // ...
public:
    // ... no user-defined copy operations ...
    ~Smart_ptr2() { delete p; }  // p is an owner!
};

void use(Smart_ptr2<int> p1)
{
    auto p2 = p1;   // error: double deletion
}

The default copy operation will just copy the p1.p into p2.p leading to a double destruction of p1.p. Be explicit about ownership:

The default value of the copy operation will only assign p1.p p2.p (which does not contain a copy of the pointing object), which leads to a double p1.p destructor. Clear ownership:

template<typename T>
class Smart_ptr3 {
    owner<T*> p;   // OK: explicit about ownership of *p
    // ...
public:
    // ...
    // ... copy and move operations ...
    ~Smart_ptr3() { delete p; }
};

void use(Smart_ptr3<int> p1)
{
    auto p2 = p1;   // OK: no double deletion
}

 

Translator's Note: p is not actually change the type of owner you can solve the problem. Note that the code of annotations by copy and move operations, and the front piece of code no.

 

Note (Note)

 

 

Often the simplest way to get a destructor is to replace the pointer with a smart pointer (e.g., std::unique_ptr) and let the compiler arrange for proper destruction to be done implicitly.

In general, to obtain destructor easiest way is to replace the smart pointer a pointer (e.g. std :: unique_ptr) and allow the compiler to provide appropriate destructor operation executed implicitly.

 

Note (Note)

 

 

Why not just require all owning pointers to be "smart pointers"? That would sometimes require non-trivial code changes and may affect ABIs.

Why not simply require that all pointers have become the owner of "smart pointer"? Because that can sometimes cause significant code changes and affect binary interface.

 

Enforcement (Suggestions)

 

 

  • A class with a pointer data member is suspect.

    Class with a pointer type data members are suspicious.

  • A class with an owner<T> should define its default operations.

    Have owner <T> class members should define the default action.

 

 

Translator's Note: owner definition is T, just increase the amount of information at the source code level, to facilitate understanding and tools to check. The compiler looks no difference before the T, there is no change at the binary level product, thus ensuring existing code can be safely introduced this practice.

 

Original link:

 

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c33-if-a-class-has-an-owning-pointer-member-define-a-destructor

 


 

I think this article helpful? Welcome thumbs up and share it with more people.

Read more updated articles, please pay attention to micro-channel public number of object-oriented thinking []

Published 408 original articles · won praise 653 · views 290 000 +

Guess you like

Origin blog.csdn.net/craftsman1970/article/details/104331826