本文发表在 rolia.net 枫下论坛In this code, class A is base class and yet it needs to know its child type B and C prior to its implementation of its own member Create()...
A most common use of static function in my career will be the service modules that interface to operating systems. Many of the operating systems are written in C or similar language so static function provides them a fixed entry point when the service module needs to hook up with a thread or callback functions. There are many existing examples if you are developing OS or OS drivers.
When you think of the word 'static', please think of something that all the instances of the class will share for their own use. For example, a system entry point, a system callback or something that is exclusively share among all the instances of the same class. Anyway, there are too many examples if you are developing OS or drivers, especially if it's RTOS or real-time system applications.
BTW, I don't have any comments on what other Rolian developers here are writing about... Many of the OS and driver developers have already decouple the underlying resource sharing with all these techniques so that the application developers don't feel they are restricted by the underlying hardware or system when using the APIs provided above the abstraction layer. So in their career context, they believe what they concluded are 'truth' that can cover what others aspect of the computer science make decisions for others... But that's not something I would appreciate.
If you want, you can think about how to implement a system callback that is shared among all the instances of the same class... That will lead you to a much better example of how to use 'static' in C++. And yet, I haven't told you the techniques to access the non-static members from a static callback, I will leave the question to all the readers if you are interested in this.
Anyway, keep digging and keep coding... if you can find it, dig deeper :)更多精彩文章及讨论,请光临枫下论坛 rolia.net
A most common use of static function in my career will be the service modules that interface to operating systems. Many of the operating systems are written in C or similar language so static function provides them a fixed entry point when the service module needs to hook up with a thread or callback functions. There are many existing examples if you are developing OS or OS drivers.
When you think of the word 'static', please think of something that all the instances of the class will share for their own use. For example, a system entry point, a system callback or something that is exclusively share among all the instances of the same class. Anyway, there are too many examples if you are developing OS or drivers, especially if it's RTOS or real-time system applications.
BTW, I don't have any comments on what other Rolian developers here are writing about... Many of the OS and driver developers have already decouple the underlying resource sharing with all these techniques so that the application developers don't feel they are restricted by the underlying hardware or system when using the APIs provided above the abstraction layer. So in their career context, they believe what they concluded are 'truth' that can cover what others aspect of the computer science make decisions for others... But that's not something I would appreciate.
If you want, you can think about how to implement a system callback that is shared among all the instances of the same class... That will lead you to a much better example of how to use 'static' in C++. And yet, I haven't told you the techniques to access the non-static members from a static callback, I will leave the question to all the readers if you are interested in this.
Anyway, keep digging and keep coding... if you can find it, dig deeper :)更多精彩文章及讨论,请光临枫下论坛 rolia.net