标签:c++
拥有静态存储周期(static storage duration)的对象将会被一直保存到程序结束。
存储类型说明符(static)用于声明对象拥有静态存储期(static storage duration)。
void func(static int tag){
cout<<tag<<endl;
{
static void nonStaticFunc(int x); // error!
}
};而这说明在其他作用域中声明静态函数和静态函数参数都是合法的,因此下面的程序是合法的。#include<iostream>
static void func(static int tag){
cout<<tag<<endl;
};
int main(int argc, char *argv[]){
func(23);
}#include<iostream>
using namespace std;
class process {
public:
static char* reschedule(){return "World!";};
};
process& g(){
process a;
cout<<"Hello ";
return a;
};
int main(int argc, char *argv[]){
cout<<process::reschedule()<<endl; // OK: no object necessary "World!"
cout<<g().reschedule()<<endl; // g() is called, "Hello World!"
}#includeusing namespace std; char* g(){return "g()";}; char* k(){return "k()";}; struct X { static char* g(){return "X::g()";}; static char* h(){return "X::h()";}; static char* w(){return "X::w()";}; }; struct Y : X { static char* g(){return "Y::g()";}; static char* w(){return "Y::w()";}; static char *i,*j,*p,*q; }; char* Y::i = g(); char* Y::j = h(); char* Y::p = X::w(); char* Y::q = k(); int main(int argc, char *argv[]){ cout<<Y::i<<endl; // output Y::g() cout<<Y::j<<endl; // output X::h() cout<<Y::p<<endl; // output X::w() cout<<Y::q<<endl; // output k() }
struct X{
static int i;
int j;
};
int X::i=sizeof(j); // errorclass X{
static void f();
void f(); // ill-formed
void f() const; // ill-formed
void f() const volatile; // ill-formed
void g();
void g() const; // OK: no static g
void g() const volatile; // OK: no static g
};
#include<iostream>
using namespace std;
struct A {
int i;
static int j;
int k;
}a={1,2};
int main(int argc, char *argv[]){
cout<<a.i<<endl; // 1
cout<<a.k<<endl; // 2
cout<<a.j<<endl; // error!
}#includeusing namespace std; struct Outer{ static char* x; struct Inner{ static char *x,*y; }; }; char* Outer::x="Outer::x"; char* Outer::Inner::x="Inner::x"; char* Outer::Inner::y=x; int main(int argc, char *argv[]){ cout<<Outer::Inner::y<<endl; // Inner::x }
void func(){
struct Local{
static int i; // error!
}x;
}程序的终止过程包含了静态对象的销毁。
struct X{
~X(){cout<<"~X()"<<endl;}
};
struct Y{
~Y(){cout<<"~Y()"<<endl;}
};
void f(){static X x;}
void g(){static Y y;}
int main(int argc, char *argv[]){
f(); // ~X()
}C++ 静态存储周期(static storage duration),布布扣,bubuko.com
C++ 静态存储周期(static storage duration)
标签:c++
原文地址:http://blog.csdn.net/kingwolfofsky/article/details/26156631