Static

1. C++

static has three methods to use.

a. As a class member

b. As a class method

c. non linkage. used in a function. it can't be accessed when the function is not called. but when the function is called again, the value will keep the previous one before it is changed in this calling.it is stored statically.

d. internal need to add static. it si stored statically. but we can set a variable with the same name in other files.

e. external  no need to add static. but it is stored statically.

 

voidStringChapter::staticTest()

{staticint id2 = 0;id2++;

}

 myapp->staticTest();

myapp->staticTest();

myapp->staticTest();

myapp->staticTest(); 

in the forth calling of statictest, the value of id2=4;

it means in c++, the static variable will be only initialize once. But can be assigned for several times!

it means, the static variable will keep to change its value since staticint id2 = 0; is called for several times.

 

For case a:


class const_test
{
public:
 static const int number;
 static int number2;
 const_test();
 void change();
 ~const_test();

};

 

const int const_test::number=7;
int const_test::number2=5;

you should initialize the static members as  <数据类型><类名>::<静态数据成员名>=<值>

for const static member, you can use  static const int number=7; directly. But for non-const static  member , you can not.

class const_test
{
public:
 static const int number=7;
 static int number2=5;
 const_test();
 void change();
 ~const_test();

static void schange();

};

D:/Projects/CPP/CPP/EFFECTIVE/const_test.h:8:21: error: ISO C++ forbids in-class initialization of non-const static member 'const_test::number2'

 

For caseb:


void const_test::schange()
{ number2++;
 number3=0;
 } 

D:/Projects/CPP/CPP/EFFECTIVE/const_test.h:9:6: error: invalid use of member 'const_test::number3' in static member function

 

2. C#

a. static field. 

b. static method.

c. static constructor.

d. static attribute.

e. static class

f. none linkage. Error!

 

 

if is not assigned be a initial value. it woud be set to 0 null false. it belong to the class instead of instance.

they would be initialized once any static fields of this class is calling. 

  1.1static readonly int a[];

  1.2a=new a[10];

  1.3a =new a [11]; //exception. static field can not be new twice.

 

I made a mistake for so long a time. Only readonly can be assign once. not static.....................................................!

 

b. static method.

if you want to use instance method. please get a instance at first. (from arguments)

if you want to use instance field, please get a instance at first as well.

 

 

c. static constructor.

it is use to initialize the class, not instance. after self initialize, the static field will be initialize in the static class.

It would be called at the first calling of this class.

 

d. static attribute.

 

e. static class

abstact sealed.

 

f. none linkage. Error!

 

In class scope. c++ and c# have the same usage of static (field and method).

For case a:

c++ static member must be initialize in declaration or as const int const_test::number=7;

c# static member must be initialize in declaration. But if you didn't give a value for no-const member, it will set to 0. for const member, it is necessary.

But for const members:

c#            need to assign a value in declararion.                  c++ Both   declaration or as const int const_test::number  would be well.

and it should be static readonly instead of static constant        

      

 

posted on 2015-04-13 22:21  shoutcharter  阅读(384)  评论(0编辑  收藏  举报

导航