“As a rule of thumb, make all your methods virtual” in C++ - sound advice?

前端 未结 5 976
野性不改
野性不改 2021-01-07 23:38

I just happened upon the statement in the title. The full quote is:

As a rule of thumb, make all your methods virtual (including the destructor, but

5条回答
  •  醉梦人生
    2021-01-08 00:15

    It can't hurt. If you don't have a subclass which redefines the function, nothing is different. I can see this technique being useful if you have a lot of inheritance going on and you might lose track of what classes inherit which.

    Personally, I don't make every class method virtual...but that's just me. Having everything virtual seems to make things look more confusing, imho.

提交回复
热议问题