C ++ core principles C.82: Do not call virtual functions in the constructor or destructor

C.82: Don't call virtual functions in constructors and destructors

C.82: Do not call virtual functions in the constructor or destructor

 

 

Reason (reason)

The function called will be that of the object constructed so far, rather than a possibly overriding function in a derived class. This can be most confusing. Worse, a direct or indirect call to an unimplemented pure virtual function from a constructor or destructor results in undefined behavior.

So far, the called function should only belong to construct the object itself, rather than may exist in a derived class of a cover function. Do very difficult to understand. The worst case, in the constructor or destructor call directly or indirectly, a pure virtual function is not implemented undefined behavior will result.

 

Example, bad (negative sample)

class Base {
public:
    virtual void f() = 0;   // not implemented
    virtual void g();       // implemented with Base version
    virtual void h();       // implemented with Base version
    virtual ~Base();        // implemented with Base version
};

class Derived : public Base {
public:
    void g() override;   // provide Derived implementation
    void h() final;      // provide Derived implementation

    Derived()
    {
        // BAD: attempt to call an unimplemented virtual function
        f();

        // BAD: will call Derived::g, not dispatch further virtually
        g();

        // GOOD: explicitly state intent to call only the visible version
        Derived::g();

        // ok, no qualification needed, h is final
        h();
    }
};

Note that calling a specific explicitly qualified function is not a virtual call even if the function is virtual.

Note: calling a specific function is not defined virtual call, even if the function is a virtual function.

See also factory functions for how to achieve the effect of a call to a derived class function without risking undefined behavior.

Reference plants function in order to understand how to achieve the effect of a derived class to call functions without having to risk causing undefined behavior.

 

Note (Note)

There is nothing inherently wrong with calling virtual functions from constructors and destructors. The semantics of such calls is type safe. However, experience shows that such calls are rarely needed, easily confuse maintainers, and become a source of errors when used by novices.

Called from the constructor and destructor virtual function itself does not have any errors. This invocation semantics is safe. However, experience has shown that such calls are rarely necessary, it is easy to disrupt the defenders, if used novice can become sources of error.

 

Enforcement (Suggestions)

  • Flag calls of virtual functions from constructors and destructors.

  • Tip virtual function from a constructor or destructor call.

Description link

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c82-dont-call-virtual-functions-in-constructors-and-destructors

 


 

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 []

发布了408 篇原创文章 · 获赞 653 · 访问量 29万+

Guess you like

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