Why does enable_shared_from_this have a non-virtual destructor?

后端 未结 3 1483
轻奢々
轻奢々 2021-02-07 04:21

I have a pet project with which I experiment with new features of C++11. While I have experience with C, I\'m fairly new to C++. To train myself into best practices, (besides re

3条回答
  •  时光取名叫无心
    2021-02-07 04:49

    I agree with Jame's description, but would add

    A virtual destructor is only required if you want to destroy an instance of that class virtually. This is not always the case, however if a base class is not intended to be destroyed virtually it should protect against it

    So I would change

    A destructor of a class that is intended for subclassing should always be virtual, IMHO.

    this to be:

    A destructor of a class that is intended for subclassing should always be virtual or protected.

提交回复
热议问题