Why is the “virtuality” of methods implicitly propagated in C++?

前端 未结 4 1069
闹比i
闹比i 2021-01-04 08:13

What is the reason for removing the ability to stop the propagation of methods virtuality?

Let me be clearer: In C++, whether you write \"virtual void foo()\" or \"v

4条回答
  •  感动是毒
    2021-01-04 08:45

    In my opinion one of the good reasons for this propagation is the virtual destructors. In C++ when you have a base class with some virtual methods you should define the destructor virtual. This is because some code may have a pointer of base class which is actually pointing to the derived class and then tries to delete this pointer (see this question for more detail). By defining the destructor in base class as vritual you can make sure all pointers of base class pointing to a derived class (in any level of inheritance) will delete properly.

提交回复
热议问题