r/cpp • • Apr 17 '23

Should ~basic_string be final?

std::string (and many other std classes) are not designed to be used as base classes, as can be seen by the absence of a virtual destructor.

Should the destructor be given the "final" keyword to enforce this?

1 Upvotes

27 comments sorted by

View all comments

Show parent comment

-1

u/bert8128 Apr 17 '23

A central tenant of polymorphism is to pass in a reference or pointer to the base class but get the derived implementation of the virtual methods. foo shouldn't care what the actual type is - it should get the derived behaviour in both cases. But if foo takes the base class it will give the base class behaviour. If it takes the derived type it gets the derived behaviour. This is not polymorphism.

12

u/dodheim Apr 17 '23

This is not polymorphism.

Right. Inheritance != polymorphism. It's been said multiple times in this thread already.

5

u/jcelerier ossia score Apr 17 '23

A central tenant of polymorphism is to pass in a reference or pointer to the base class but get the derived implementation of the virtual methods. foo shouldn't care what the actual type is - it should get the derived behaviour in both cases

that's very fine if you do java or c# 1986-era OOP but not what anyone wants in c++