Virtual function keyword

后端 未结 2 935
暗喜
暗喜 2021-01-24 10:41

Is there any difference between declaring inherited virtual function in a child class with the \"virtual\" keyword or not, considering I want t

相关标签:
2条回答
  • 2021-01-24 11:23

    Keeping the virtual key word before the overridden member function in the derived class is optional. Run-time polymorphism works only for pointers or references.

    0 讨论(0)
  • 2021-01-24 11:29

    Overriding functions in derived classes are implicitly declared "virtual" if the corresponding function in the base class is virtual. Just make sure you got the exact same signature, or you might inadvertently hide the original function and declare a new one!

    In C++0x, feel free to make liberal use of the override specifier.

    Your two "Why?" questions are because of slicing; you're making new, copy-sliced objects of type A. Note that in B x; static_cast<A>(x); the cast is the same as saying A(x).

    0 讨论(0)
提交回复
热议问题