• 516052 (unregistered)

    "combines the syntactic elegance of C with the run-time efficiency of Smalltalk"

    God. This had me laughing.

  • (nodebb)

    The type check seems to be unnecessary anyway. GetFirstTentacle is clearly allowed to return null, even if called against an octopus or squid (presumably some of them have lost all their tentacles). So there should be no issue skipping the type check and calling GetFirstTentacle anyway. If it's a cat, it should just return null - we don't care what it is, just that it has no tentacles to steal. Which also means that if someone adds a tentacled cat to the system it won't break this function. .

  • (nodebb)

    // only Octopus and Squid can have tentacles

    Sadly incorrect. An octopus has arms rather than tentacles, and there are plenty of species that aren't octopuses or squids that have tentacles of some sort, including one species of mammal, the star-nosed mole.

    Their comment highlights their misunderstanding of the function; dynamic_cast can be used to crosscast. In other words, dynamic_cast<Dog*>(my_cat) will actually work, because (in this example) Dog* and Cat* share a common base.

    It will, indeed, work, in that it will do what it is supposed to, and cross-casting like that is not part of what it is supposed to do. The return value will be NULL or, more modernly, nullptr, if you try to cast between two classes that are not part of the same inheritance tree. (If there's a class DogCat which derives from Cat and from Dog, and you have a pointer to the Cat part of a DogCat, then you can dynamic_cast from Cat * to Dog * and you'll get the Dog part of the same object that the Cat part is part of. But in general, you can't do an arbitrary crosscast.

    This code attempts to take an objectPointer and first, force it into a pointer to our base type (Organism), using a C-style cast.

    Bad programmer! There are legitimate uses for C-style casts in C++, but a certain amount of care is needed, and they shouldn't be used as a substitute for the correct C++-style cast (in this case, it's not necessary anyway, since dynamic_cast will search through the inheritance tree of the object from any starting point). (There is only one thing that you can do only with a C-style cast, namely (static) casting to a private base class that's actually a base of the requested object, which is something you shouldn't do anyway.)

    a const_cast (which only alters the constness of the type)

    More correctly, const_cast removes "cv qualifiers", i.e. const and/or volatile.

  • (nodebb) in reply to SteelCamel2

    Also curious as to how it treat sentient tentacles that come in green and purple hues.

  • (nodebb)

    I think some people missed the point. Instead of having an override function that says "you're an animal, do you support tentacles, yes/no?" the base class is checking what derived class was passed in to decide. The code is anonymized so it could have been something else not tentacle-related like "can you fly?", "do you breathe oxygen?", "can you sting people?", etc....

Leave a comment on “A Type of Type Safety”

Log In or post as a guest

Replying to comment #705255:

« Return to Article