全局变量和全局静态变量的区别
2015-06-30 22:24
218 查看
1)全局变量是不显式用static修饰的全局变量,全局变量默认是有外部链接性的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量。
2)全局静态变量是显式用static修饰的全局变量,作用域是声明此变量所在的文件,其他的文件即使用extern声明也不能使用。
2)全局静态变量是显式用static修饰的全局变量,作用域是声明此变量所在的文件,其他的文件即使用extern声明也不能使用。
// Book.h NSString *const bookName = @"Cocoa"; @interface Book : NSObject @end // Student.h extern NSString *bookName; @interface Student : NSObject @end // Student.m #import "Student.h" @implementation Student - (NSString *)description { return [NSString stringWithFormat:@"%@", bookName]; } @end #import "ViewController.h" #import "Student.h" @interface ViewController () @end // viewController.m @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Student *s = [[Student alloc] init]; NSLog(@"s = %@", s); } // 结果: s = Cocoa 但是如果写成 static NSString *const bookName = @"Cocoa"; 再编译就会报错了。
相关文章推荐
- libxml2 的一些用法
- 【再思考】排序算法——归并排序
- 数组
- spring+struts+hibernate
- 【Android】组件生命周期
- MVC几种传值方式
- 网络编程
- 泛型和面向对象C++
- 深入浅出JMS(一)--JMS基本概念
- java自带的MD5加密 以及jsMD5加密
- debian命令行安装sqlite3
- 如何理解: java的import ???
- NSDictionary和NSMutableDictionary的相关总结
- 深入浅出JMS(一)--JMS基本概念
- WSGI的理解
- TFS 2015新功能之一,当前迭代查询标记
- 微博舆情 之 特定话题情感分析
- iOS and OS X Network Programming Cookbook |Creating an echo server(BSD)
- 【一些事晚报】知道苹果实习生的工资后,我哭了
- 软件包javax.servlet不存在的解决办法