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

前端 未结 5 1007
野性不改
野性不改 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:28

    IMHO, this is a good rule of thumb for beginners with C++. It's not really harmful except in very specific scenarios (ones faced by programmers who know exactly what the tradeoffs of the virtual keyword are), and is one less thing to think about.

    However, like every rule of thumb, it's oversimplifying the situation, and when one grows to think about how to communicate to other programmers what methods may or may not be good candidates for redefining, one has outgrown the rule of thumb.

提交回复
热议问题