Reputation: 5580
Should interfaces such as NonCopyable and NonMovable provide virtual destructors or not, and just declare the destructor protected?
I can't imagine anyone wanting to store objects as NonCopyable/NonMovable and use them in a polymorphic way like this.
Upvotes: 0
Views: 238
Reputation: 45704
The traits NonCopyable and NonMovable are orthogonal to each other and to polymorphic use.
So why shouldn't someone want to mix-and-match in any combination?
Anyway, the proper way to avoid the issue is declaring those classes destructor protected, and only inherit them private. Neil Kirk is right about that.
Also, since C++11, you can explicitly declare any function you want deleted, which is the preferred way.
Upvotes: 0
Reputation: 234875
With C++11, you can delete constructors and destructors, rendering many NonCopyable / NonMovable idioms obsolete:
YourClass() = delete; /*deletion of default constructor*/
YourClass(const YourClass&) = delete; /*deletion of copy constructor*/
~YourClass() = delete; /*deletion of the destructor of YourClass*/
etc. You'll get a compile time failure if any code requires these.
Upvotes: 1
Reputation: 30489
No non copyable base won't require a virtual destructor. But yes the class that extends this non-copyable may required virtual destructor.
If you are using boost, you can inherit from noncopyable.hpp
Upvotes: 2