C++基础--static的用法
首先,看看变量的存储:
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的用法的更多相关文章
- EasyUI中Base(基础)的基本用法
EasyUI中Base(基础)的用法 一.Base(基础) 1.parser 解析器 2.easyloader 简单加载 3.draggable 拖动 4.droppable 放置 5.resizab ...
- Java中static的用法
static静态,作为修饰符,最初是由c引入,一开始static表示退出一个块后依然存在的局部变量.随后,static表示不能被其他文件访问的全局变量和函数.到了C++和java,static表示属于 ...
- ava下static关键字用法详解
Java下static关键字用法详解 本文章介绍了java下static关键字的用法,大部分内容摘自原作者,在此学习并分享给大家. Static关键字可以修饰什么? 从以下测试可以看出, static ...
- scrapy之基础概念与用法
scrapy之基础概念与用法 框架 所谓的框架就是一个项目的半成品.也可以说成是一个已经被集成了各种功能(高性能异步下载.队列.分布式.解析.持久化等)的具有很强通用性的项目模板. 安装 Linux: ...
- C# static的用法详解
C# static的用法详解 有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 n ...
- java基础 -- 关键字static的用法
static关键字的基本作用就是方便在没有创建对象的情况下调用类的方法/变量, static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static ...
- 【C#基础】static 关键字用法小结
静态变量 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 有时候,我们希望 ...
- static的用法
首先,看看变量的存储: int global ; int main() { int stackStore ; int heapStore* = (int *)malloc(sizeof(int)); ...
- static之用法
本文转载于http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html 在C语言中,static的字面意思很容易把我们导入歧途,其实它 ...
随机推荐
- jQuery 遍历 - closest() 方法
jQuery 遍历参考手册 实例 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("c ...
- 网址访问量统计插件 FlagCounter
网址或博客访问量统计插件 ---> FlagCounter. 网址:http://s01.flagcounter.com/more/ERP2/
- php Apache No input file
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 变为 RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
- npm 安装 sass=-=-=
先按照 cnpm .....因为外网安不上... cnpm install node-sass --save-dev cnpm install sass-loader --save-dev
- mysql id_logfile 日志
ib_logfile 文件原理 ib_logfile0 是innodb中事务日志,记录系统的回滚,重做日志,记录的是文件的物理更改,存放位置my.ini 中的 datadir="D:\php ...
- no git binary found in $path(已解决,但是还有疑问)
跟同行研究个项目代码,他把代码打包发我后,我解压到本地,路径和我本地个人项目路径基本相同, 但是当执行npm install时,就报了 no git binary found in $path ,这个 ...
- Randy Pausch’s Last Lecture
he University of Virginia American Studies Program 2002-2003. Randy Pausch ...
- [转] 理解SVG transform坐标变换
http://www.zhangxinxu.com/wordpress/2015/10/understand-svg-transform/
- 转 Oracle]如何在Oracle中设置Event
https://www.cnblogs.com/gaojian/p/7619960.html 为了调查Oracle 的故障,可以通过设置event ,来了解详细的状况.方法如下: ■ 如果使用 SPF ...
- spring框架中由FactoryBean获取JedisCluster实例
spring配置文件: <bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisCluste ...