2009/05/18

[C++] (原創) 存取class裡的static variable

static variable只會被初始化一次的變數,因此我們可以設計一個負責統計資訊的static class。除了static variable,也可將function設為static,以方邊使用。如下例,當要重新reset值時,可以呼叫Statistics::reset();即可。而在存取變數時也只要Statistics::Value[0]即可。達到類似Singleton Pattern的效果。

(click to enlarge)

Ref:
[1] http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
[2] http://weblogs.asp.net/whaggard/archive/2004/11/05/252685.aspx
[3] http://en.wikipedia.org/wiki/Singleton_pattern

沒有留言:

Buddhism and Software Developer

In today's fast-paced society, we are often surrounded by work, goals, and external pressures. However, the wisdom found in Buddhism off...