extern的用法
extern作为外部变量扩展的用法:
1. 主要作用是扩展变量或者函数的应用范围;
2. extern的用法是相对于全局变量而言;
3. 在看到extern这个关键字的时候说明这个变量已经在别的源文件中声明;
注意:变量的声明只能在源文件中(.cpp .c),但是对于extern声明的文件没有限制,可以在.h,也可以在.cpp中声明;
当然作为函数变量声明的用法也是一样的,只能作用于全局函数,也只能在源文件.cpp中声明;
一、extern作为外部变量的用法:
Func.h
#pragma once
class Func
{
public:
Func();
~Func(); void printX();
};
Func.cpp
#include "Func.h"
#include <stdio.h> int x;
Func::Func()
{
x = ;
} Func::~Func()
{
} void Func::printX()
{
printf("value of x in Func is %d", x);
}
FuncExtern.h
#pragma once extern int x;
class FuncExtern
{
public:
FuncExtern();
~FuncExtern(); void printXExtern();
};
FuncExtern.cpp
#include "FuncExtern.h"
#include <stdio.h> FuncExtern::FuncExtern()
{
} FuncExtern::~FuncExtern()
{
} void FuncExtern::printXExtern()
{
printf("value of x in FuncExtern is %d", x);
}
main.cpp
#include <stdio.h>
#include <string.h>
#include "Func.h"
#include "FuncExtern.h" int main()
{
Func *base = new Func();
base->printX();
FuncExtern *externFunc = new FuncExtern();
externFunc->printXExtern(); return 0;
}
输出结果为:
二、extern作为外部函数的用法:
Func.h
#pragma once class Func
{
public: Func();
~Func(); };
Func.cpp
#include "Func.h"
#include <stdio.h> void printX()
{
int x = 5;
printf("value of x in Func is %d\n", x);
} Func::Func()
{
} Func::~Func()
{
}
FuncExtern.h
#pragma once extern void printX();
class FuncExtern
{
public:
FuncExtern();
~FuncExtern(); void printXExtern();
};
FuncExtern.cpp
#include "FuncExtern.h"
#include <stdio.h> FuncExtern::FuncExtern()
{
} FuncExtern::~FuncExtern()
{
} void FuncExtern::printXExtern()
{
printX();
}
main.cpp
#include <stdio.h>
#include "Func.h"
#include "FuncExtern.h" int main()
{
FuncExtern *externFunc = new FuncExtern();
externFunc->printXExtern(); return 0;
}
输出结果为:
通过以上分析我认为extern唯一的用法是你能使用一个文件里全局变量而不需要include这个头文件;
extern的用法的更多相关文章
- extern "c"用法解析
转自: extern "c"用法解析 - 简书 引言 C++保留了一部分过程式语言的特点,因而它可以定义不属于任何类的全局变量和函数.但是,C++毕竟是一种面向对象的程序设计语言, ...
- 《OOC》笔记(1)——C语言const、static和extern的用法
<OOC>笔记(1)——C语言const.static和extern的用法 C语言中const关键字用法不少,我只喜欢两种用法.一是用于修饰函数形参,二是用于修饰全局变量和局部变量. 用c ...
- extern "C" 用法解析
extern "c"用法解析 作者 作者Jason Ding ,链接http://www.jianshu.com/p/5d2eeeb93590 引言 C++保留了一部分过程式语言的 ...
- 变量的声明和定义以及extern的用法
变量的声明和定义以及extern的用法 变量的声明不同于变量的定义,这一点往往容易让人混淆. l 变量 ...
- 关于extern的用法
extern表示该变量或者函数时在另一个地方定义了. 在C++编程中,如果将程序分为多个文件,则需要有在文件间共享代码的方法,这时如果一个变量或者函数需要在多个文件中使用,则可以使用extern来声明 ...
- static和extern的用法小结
以前写程序是,基本不管static和extern,一个工程文件也只有一个c文件.今天尝试用多个文件来写,自然就涉及到这两个关键词的使用,自己查了些资料,并且做了些实验,总结如下. extern的用法 ...
- 命名空间 extern的用法 static全局变量
std是标准库中的命名空间: 关于extern的用法可以参考文献http://blog.163.com/sunjinxia%40126/blog/static/94984879201312145021 ...
- ZT --- extern "C"用法详解 2010-08-21 19:14:12
extern "C"用法详解 2010-08-21 19:14:12 分类: C/C++ 1.前言: 时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus ...
- C++基础--extern的用法
extern作为外部变量扩展的用法: 1. 主要作用是扩展变量或者函数的应用范围: 2. extern的用法是相对于全局变量而言: 3. 在看到extern这个关键字的时候说明这个变量已经在别的源文件 ...
随机推荐
- Bootstrap的学习
Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT <link href="http://c ...
- salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)
salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesforce中的数据库使用的是Force.com 平台的数据库,数据表一行数据可以理解成一个sObject变量 ...
- DateUtil
//有些地方需要修改 import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDate ...
- SQLServer性能分析
SQLServer性能分析 当数据库出现性能问题,应用出现运行缓慢的时候,下面这个东东能让你如获至宝 create table #sp_who2 ( SPID int ,status varchar( ...
- cordovas禁止横屏
cordovas禁止横屏 官网 http://cordova.apache.org/docs/en/latest/config_ref/index.html#preference 配置config.x ...
- LLBL Gen Template Studio 2.x
Template Studio for LLBLGen Pro released Today we released Template Studio, a full-featured IDE for ...
- switch判断注意点
if判断,如果判断的两个值类型不同,会继续隐性转换,==,当然如果使用===就不会. 1 if(2=="2"){ 2 console.log("true"); ...
- 【WP8.1开发】基于应用的联系人存储
上一篇文章所吹的牛是访问系统(手机)上的联系人,当然那只是读不能改,这是自然的,要是让你能随便修改用户的联系人信息的话,那后果很严重,有些恶意开发者就有可能把”你的户口改成猪“. 但是,API也允许应 ...
- JS 关于(function( window, undefined ) {})(window)写法的理解
JS 关于(function( window, undefined ) {})(window)写法的理解 [网络整理] (function( window, undefined ) {})(windo ...
- .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引
系列文章索引: .NET面试题解析(01)-值类型与引用类型 .NET面试题解析(02)-拆箱与装箱 .NET面试题解析(03)-string与字符操作 .NET面试题解析(04)-类型.方法与继承 ...