Difference among static, const and static const:

static variable: we can't reinitialize the variable but we can update
const variable: we can't update the variable
static const variable: we can't reinitialize and can't update the variable


static:

Local variables have automatic duration, which means they are destroyed when the block they are declared in goes out of scope.
Fixed duration variable is one that retains it’s value even after the scope in which it has been created has been exited! Fixed duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program. (Similar to Common Block in Fortran, but much safer.)

#include <iostream>

void IncrementAndPrint1()
{
    
using namespace std;
    
int nValue = 1// automatic duration by default
    ++nValue;
    cout 
<< nValue << endl;
// nValue is destroyed here


void IncrementAndPrint2()
{
    
using namespace std;
    
static int s_nValue = 1// fixed duration //initial once and for all
    ++s_nValue;
    cout 
<< s_nValue << endl;
// s_nValue is not destroyed here, but becomes inaccessible



int main()
{
    IncrementAndPrint1();
    IncrementAndPrint1();
    IncrementAndPrint1();

    IncrementAndPrint2();
    IncrementAndPrint2();
    IncrementAndPrint2();
}


const variable and const method:

 1 int main()
 
2 {
 
3     const int i = 10;
 
4 
 
5     i = 3;            // ERROR - we can't change "i"
 6 
 
7     int &= i;       // ERROR - we promised not to
 8                       // change "i" so we can't
 9                       // create a non-const reference
10                       // to it
11 
12     const int &= i; // fine - "x" is a reference
13                       // to "i"
14 
15     return 0;
16 }


 1 class Foo
 
2 {
 
3 public:
 
4     /*
 5      * Modifies m_widget and the user
 6      * may modify the returned widget.
 7      
*/
 
8     Widget *widget();
 
9 
10     /*
11      * Does not modify m_widget but the
12      * user may modify the returned widget.
13      
*/
14     Widget *widget() const;
15 
16     /*
17      * Modifies m_widget, but the user
18      * may not modify the returned widget.
19      
*/
20     const Widget *cWidget();
21 
22     /*
23      * Does not modify m_widget and the user
24      * may not modify the returned widget.
25      
*/
26     const Widget *cWidget() const;
27 
28 private:
29     Widget *m_widget;
30 };
31 
32 int main()
33 {
34     Foo f;
35 
36     Widget *w1 = f.widget();  // fine
37 
38     Widget *w2 = f.cWidget(); // ERROR - "cWidget()"
39                               // returns a const value
40                               // and "w2" is not const
41 
42     const Widget *w3 = f.cWidget(); // fine
43 
44     return 0;
45 }


static const:

For POD types where the right-hand side is constant, you won't see any difference. ie,

1
2
3
4
void foo() {
            static const double PI = 3.14;
            const double PI = 3.14;
            }


But if the right-hand side is non-trivial, you could see a performance difference:

1
2
3
void foo() {
            static const int fib100 = fibonacci( 100 );
            const int fib100 = fibonacci( 100 );


In the first case, fib100 is computed exactly once during the lifetime of the program. In the second case, it is computed each time the foo() is called. If fibonacci() is poorly coded, this could result in a significant performance degradation.

So it really comes down to how much work it takes to do the initialization.




Posted on 2011-08-05 11:43 于江浩 阅读(258) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理