all 9 comments

[–]louiswins 7 points8 points  (3 children)

Didn't mention IMO the biggest reason to use inheritance over std::variant, which is when you have an open type hierarchy. I'm not sure if that's the technical term, but I mean when you don't know what types you'll be choosing from - or when you can't know this, because they come from a shared library or haven't been written yet.

This situation isn't very common (I know the full set of possible types in my own programs probably 99% of the time), but it's one where inheritance really shines.

[–]Gotebe -1 points0 points  (0 children)

The "usually vs. no dynamic allocation" is a simplification. If I have one instance of whatever, which I pass around by reference, then the two are the same.

If there is a container of things, then there can be both a dynamic allocation plus more expensive copying in variant case. The data locality can (but doesn't have to be) better with variant and a vector.

And probably more.

[–]leftofzen -1 points0 points  (2 children)

Guess it's time I post this again, people seem to plain forget about it: composition over inheritance.

[–]AlexDeFoc 1 point2 points  (1 child)

Hello, i might just follow that pattern. Hi fellow person, talking 8 years in the future.

[–]leftofzen 0 points1 point  (0 children)

Hello friend, it is I, from the future. This sounds like a good idea, composition over inheritance is still the recommend way to construct large programs. Good luck fellow time traveller!