- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
God. This had me laughing.
Edit Admin
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. .
Edit Admin
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.
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
NULLor, 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 fromCat *toDog *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.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_castwill 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.)More correctly, const_cast removes "cv qualifiers", i.e.
constand/orvolatile.Edit Admin
Also curious as to how it treat sentient tentacles that come in green and purple hues.
Edit Admin
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....
Edit Admin
They're already applying that optimization to the animal you're stealing a tentacle from. They "have" to do it here to enforce the same constraint on the animal you're stealing the tentacle to.
Edit Admin
Yes. Complaining about the type of cast or how the class of a parameter is being determined is a bit like complaining about the colour of the iceberg that hit the Titanic.
There should be a definition of
getFirstTentacle()in the base class that always returnsnulland it should be overridden in classes that have tentacles.However
It does look like any
Animalcan attach a tentacle to itself, even if it wasn't born with any, so perhaps the implementation should include a tentacle collection in the base class which starts empty for mostAnimals and is populated with 8 or 10 tentacles in the constructors forOctopusandSquidrespectively. The functions can then be fully defined inAnimalAdmin
Dang! My octopus lost an arm! I'm checking the manual, but I can't find a number for tentacle support.
Edit Admin
To Remi's opening point: Back in the '90s, when I was first introduced to OOP, tutorials indeed liked to use animals, as well as vehicles. (I especially recall bicycles, but cars were in there, too.) None of it made any sense whatsoever, until I found something that explained object-oriented programming in terms of programming. It made perfect sense then. Gosh. Imagine that.
Edit Admin
Has nobody noticed that the code is checking if
thisis an Octopus or Squid, and notanimal? An octopus can steal a tentacle from a cat, but a cat can't steal a tentacle from an octopus.