4 ms·
My favorite: slicing. That one blew my mind when I found out. You can rip off a chunk of an object by accident... and nothing happens whatsoever. There's no wa
by dale_glass 5y ago
My favorite: slicing.
That one blew my mind when I found out. You can rip off a chunk of an object by accident... and nothing happens whatsoever. There's no warning from the compiler.
You can stuff a Derived into a std::list<Base>, it'll work just fine, and if Derived happened to say, hold a pointer, it'll happily vanish into the ether.
Besides the amount of confusion that can ensure from this, it means you can have problems if you want to introduce inheritance into a place that didn't have it before.
Especially if it's some sort of external component. You think you're clever inheriting from a framework class and adding some data on top? Nope, the internal structure you don't control doesn't want to cooperate with that plan.
- jcelerier 5y ago> There's no warning from the compiler You can use clang-tidy to warn / error on that: https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-slicing.html https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidel...
- slavik81 5y ago> You can rip off a chunk of an object by accident... and nothing happens whatsoever. There's no warning from the compiler. Slicing is a bad name for this phenomenon, because what you've described is what the name implies, but that is not what is happening. The original object is completely unchanged. Nothing was ripped off of it. Constructors create new objects.
- gpderetta 5y agoI think slicing gets a bad name because of the old pervasive OOP mentality. Slicing is perfectly fine and expected in a value-type oriented program.