Why can a static member function only be declared static inside the class definition and not also in its own definition?

我与影子孤独终老i 提交于 2019-12-03 10:44:36

Your class definition (in the header file) will provide the function with whatever propreties are necessary :

  • static
  • inlined
  • virtual

Considering that every further object will look at your class definition using the .h then it makes sense that these properties to be defined there.

Furthermore, each function from the class will mentain it's property in the derived classes (for example you need to declare the destructor virtual only in your base class, every subsequent inheritance will take the destructor as virtual).

It makes no sense to redeclare these properties in your implementation body .

Having to declare function proprieties in both .h and .cpp files would actually lead to allot of problems. Imagine this scenario : you declare a function as virtual in a .h file, and as static in the .cpp file. What will the compiler make that function ? virtual or static ? (or more likely a compile error , but the compiler error will just urge you to match in your .cpp file the declaration in the header. You cannot overload a function according to "static" or "virtual").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!