【翁恺】25-静态对象

static 在C语言中有两层含义:1. 存储是持久存储的  2. 访问是受限的

static in c++

C++ 中更复杂,因为有成员变量和成员函数。 C 中 全局变量static 那么该变量就只在.c 文件内有效,本地变量 static 那么该变量就是持久存储,static 的本地变量就是全局变量,static 函数只能在这个 .c 内被访问。 C++ 还有 static 成员变量 和 static 成员函数。

two basic meanings

  • static storage
    • allocated one at fixed address
  • visibility of a name
    • internal linkage
  • don't use static expect inside functions and classed

uses of "static" in c++

static free functionsinternal linkage(deprecated)
static global variablesinternal linkage(deprecated)
static local variablepersistent storage
static member variableshared by all instances
static member functionshared by all instances,can only access static member variables

global static hidden in file   C 语言中

编译器可以过,但是链接不行。 加上static, 变量 s_local 和 函数 hidden,只有在文件1 中可以被访问。

static inside functions  static在函数里面

  • value is remembered for entire program   static 本地变量其实就是全局变量

  • initialization occurs only once    只能在函数内部被访问

  • example

    • count the numbers times the function has been called

      void f(){
          static int num_calls = 0;
          ...
          num_calls++;
      }

static applied to objects   static本地变量类型是个类的话?

它在哪里?   内存

它什么时候被构造出来的?

  • suppose you have a class

    class X{
        X(int ,int);
        ~X();
        ...
    };
  • and a function with a static X object

    void f(){
        static X my_X(10,20); // 可以所有对象都有一个隐藏变量来表示是否初始化过
        ...
    }

    它在哪里和什么时候初始化是不一样的,内存在link的时候已经被分配好了,内存里的变量需要去初始化的,C++可以利用的一点是vPtr,如果如果vPtr被初始化过那么它是有效的东西--vtable的地址,否则是乱七八糟的东西,不是所有的类都有vPtr,目前的格局是实现不了的(通过类的某个已有的成员是实现不了的,想法不错,将来可以自己实现OOP语言)。在java中没有被初始化的东西是不能得到的,只有通过new才行

static applied to objects...

  • construction occurs when definition is encountered
    • constructor called at-most once       构造只发生一次
    • the constructor arguments must be satisfied
  • destruction takes place on exit from program
    • compiler assures LIFO order of destructors

conditional construction

  • example:conditional construction

    void f(int x){
        if(x>10){
            static X my_X(x,x*21);
            ...
        }
    }
  • my_X

    • is constructed once,if f() is ever called with x>10
    • retains its value
    • destroyed only if constructed

本地变量,析构在离开函数时候发生;new delete 时候发生;全局变量 程序结束时候发生。

global objects   全局对象

  • consider

    #include "X.h"
    X gloable_x(12,34);
    X gloable_X2(8,16);

空间:全局数据区;什么时候分配空间:编译或链接时候;什么时候构造:在main()之前。什么调用析构:main() 结束时候或调用了 exit(),就是程序结束时候。

  • constructors are called before main() is entered

    • order controlled by appearance in fine
    • in this case,global_x before global_x2
  • main() is no longer the first function called     程序跑的时候main() 并不是第一个运行的

    • main() exits
    • exit() is called

static initialization dependency     

多个 .cpp 文件,每个文件内都有全局变量,这些全局变量谁先初始化,没有规定,可能每次编译都不一样。它们之间可能有依赖的。跨文件的初始化顺序不行。

解决:1  别这么干   2. 所有的有依赖全局变量放在一个地方  3.像 java 中没有全局变量

  • order of construction within a file is known
  • order between files is unspecified!
  • problem when non-local static objects in different files have dependences
  • a non-local static object is:
    • defined at global or namespace scope
    • declared static in a class
    • defined static at file scope

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值