How do I cast a parent class as the child class

后端 未结 8 1973
深忆病人
深忆病人 2020-12-29 19:46

It\'s been a while since I have had to write C++ code and I\'m feeling kind of stupid. I\'ve written code that is similar to, but is not exactly, the code b

8条回答
  •  无人及你
    2020-12-29 20:41

    I think the problem isn't with how your trying to do the cast, but with why you want to cast in the first place. The code makes no sense -- even if it was syntaxically valid. You;re trying to cast a "fruit" into an "apple" in a context where it's easy to prove that you don't actually have an apple. Dynamic casts and similar are only useful when you have a pointer to a "fruit" that you have reasons to thing is also an "apple".

提交回复
热议问题