首先,看看变量的存储:

int global ;
int main()
{
int stackStore ;
int heapStore* = (int *)malloc(sizeof(int));
}

变量global存储在全局数据存储区,stackStore存储在栈中,heapStore存储在堆中;

static作为静态修释符用法:

1.static可以用来修饰变量,也可以用来修饰函数,其用法相似;

2. static可以静态的呈现一个变量,在作用范围内不会改变变量的值;

3. 但是如果函数的局部变量复写了变量的值,那么这个值在当前局部函数内有效; 若出了当前局部范围,static的值生效;

例一, static在全局范围内,用include扩展static的作用范围, 用extern扩展函数的作用域:

107.h

#ifndef _107H_
#def _107H_ extern void func();
#endif

107.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "107.h" voic func()
{
x = 12;
printf("%d\n", x);
}

108.h

#ifndef _108H_
#def _108H_ extern void func1();
#endif

108.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "108.h" voic func1()
{
x = 56;
printf("%d\n", x);
}

109.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "107.h"
#include "108.h" int main()
{
func();
func1();
printf("%d", x);
}

输出结果为:

例二,static在当前文本作用域,用extern扩展函数的作用域:

file1.h

#ifndef _FILE1_
#define _FILE1_ extern void func1();
extern void func2(); #endif

file1.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" static char* hello = "hello world!";
void func1()
{
printf("%s\n", hello);
} void func2()
{
hello = "changed world!";
printf("%s\n", hello);
}

file2.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" int main()
{
func1();
func2(); return ;
}

输出结果:

!!! 若将static char* hello = "hello world!"放入func1, 如下;

file1.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" void func1()
{
static char* hello = "hello world!";
printf("%s\n", hello);
} void func2()
{
hello = "changed world!";
printf("%s\n", hello);
}

那就会出错,错误为:

C++基础--static的用法的更多相关文章

  1. EasyUI中Base(基础)的基本用法

    EasyUI中Base(基础)的用法 一.Base(基础) 1.parser 解析器 2.easyloader 简单加载 3.draggable 拖动 4.droppable 放置 5.resizab ...

  2. Java中static的用法

    static静态,作为修饰符,最初是由c引入,一开始static表示退出一个块后依然存在的局部变量.随后,static表示不能被其他文件访问的全局变量和函数.到了C++和java,static表示属于 ...

  3. ava下static关键字用法详解

    Java下static关键字用法详解 本文章介绍了java下static关键字的用法,大部分内容摘自原作者,在此学习并分享给大家. Static关键字可以修饰什么? 从以下测试可以看出, static ...

  4. scrapy之基础概念与用法

    scrapy之基础概念与用法 框架 所谓的框架就是一个项目的半成品.也可以说成是一个已经被集成了各种功能(高性能异步下载.队列.分布式.解析.持久化等)的具有很强通用性的项目模板. 安装 Linux: ...

  5. C# static的用法详解

    C#   static的用法详解 有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 n ...

  6. java基础 -- 关键字static的用法

    static关键字的基本作用就是方便在没有创建对象的情况下调用类的方法/变量, static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static ...

  7. 【C#基础】static 关键字用法小结

    静态变量 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 有时候,我们希望 ...

  8. static的用法

    首先,看看变量的存储: int global ; int main() { int stackStore ; int heapStore* = (int *)malloc(sizeof(int)); ...

  9. static之用法

    本文转载于http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html 在C语言中,static的字面意思很容易把我们导入歧途,其实它 ...

随机推荐

  1. django中给ajax提交加上csrf

    代码来自djangoproject网站 在html中的script标签下插入下面代码 在html文档加载时候运行下面代码,并且使用$.ajaxSetup设置ajax每次调用时候传入的数据,$.ajax ...

  2. 什么是LINQ

    LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…where片段 from子句let子句w ...

  3. 使用TortoiseSVN新建及合并分支图文教程

    打开trunks目录,在trunks目录下新建两个文本文件A.java,B.java:   打开A.java输入以下内容: B.java文件可以随机输入些,本例中主要用于观察后续是否变化. 两个文件编 ...

  4. 洛谷 P1546 最短网络 Agri-Net(最小生成树)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P1546 首先不难看出这道题的思想是用了最小生成树,但是这道题有难点: 1.读题读不明白 2.不会读入 ...

  5. Hibernate学习笔记(二)—— 实体规则&对象的状态&一级缓存

    一.持久化类 1.1 什么是持久化类? Hibernate是持久层的ORM映射框架,专注于数据的持久化工作.所谓的持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么是持久化,什么又是持 ...

  6. POJ_2456 Aggressive cows 【二分求最大化最小值】

    题目: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are l ...

  7. vi下搜索文本

    ) /user ) n 下一个匹配 ) N 上一个匹配 ) ?user 从结尾开始搜索 ) :nohlsearch 关闭高亮显示6) :100 跳转到第100行

  8. [转] Java:对Scanner的useDelimiter()方法的疑问

    [From]https://segmentfault.com/q/1010000003885362 Windows下,我们在键盘上按下Enter键,实际上输入的是回车和换行两个字符:\r\n,ASCI ...

  9. PIE SDK内存栅格数据的创建

    1. 功能简介 目前在地理信息领域中数据包括矢量和栅格两种数据组织形式.每一种数据有不同的数据格式,目前PIE SDK支持多种数据格式的数据创建,下面对内存栅格数据格式的数据创建功能进行介绍. 2.  ...

  10. vue组件(持续更新)

    1.vee-validate :vue的表单验证组件 网友博客介绍:https://www.cnblogs.com/xxwang/p/6104715.html