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的字面意思很容易把我们导入歧途,其实它 ...
随机推荐
- 网页footer背景(stick footer布局)
今天遇到了一个有意思的问题,想在网站的foot里面加入一张背景图片,并且在footer的底部写下一些内容于是乎在footer添加了background,并设置了footer的大小 先说一下开始的做法: ...
- 为Arch Linux添加鼠标支持(gpm)
gpm的安装 在Arch Linux中安装gpm $ pacman -S gpm 如果你正在使用触控板,需要安装一下插件 $ pacman -S gpm xf86-input-synaptics 需要 ...
- screen虚拟终端
screen命令相当于后台执行(虚拟终端) 用法:在一些要执行很久的操作,比如我有个文件有10个G,要传输一天左右,你如果是直接传输,万一你的连接断了.是不是意味着你的操作白费的呢,这时我们可以打开一 ...
- vue 子页面,向父页面 传值...
子组件 通过 事件 向父组件传值..... 父组件 方法: methods: { appendData: function (list) { console.log(list); for (var i ...
- centos上安装theano和Lasagne
1.安装theano所需的包 sudo yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ ...
- POJ_2010 Moo University - Financial Aid 【堆预处理】
一.题面 POJ2010 二.分析 堆预处理 首先可以考虑吧随便取一个点,判断两侧的最小的总费用是多少,然后相加判断是否满足条件.如果直接判断会超时,所以需要用大根堆预处理一下.先看从分数最小的往最大 ...
- artDialog不能拖拽的问题
需要引用下面这个文件: https://github.com/aui/artDialog/edit/master/dist/dialog-plus.js
- POJ - 3263 差分+前缀和
只需不断维护相对值的前缀就能得到解 这种思想第一次是在树状数组区间更新那里看到的,由于题目要求是1~n所以直接可以用前缀和维护 注意不能直接-1 +1 还有POJ的数据..要不是书里有提谁知道会这么毒 ...
- 利用touchslide实现tab滑动切换
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Python I/O及FIle方法
一.文件操作 文件的编码格式: ASCII与UNICODE: 计算机有256个ASCII字符(8个0/1的排列组合方式一共有256种, 2**8) UTF-8是UNICODE的一种编码格式,计算机中使 ...