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的用法的更多相关文章

  1. extern "c"用法解析

    转自: extern "c"用法解析 - 简书 引言 C++保留了一部分过程式语言的特点,因而它可以定义不属于任何类的全局变量和函数.但是,C++毕竟是一种面向对象的程序设计语言, ...

  2. 《OOC》笔记(1)——C语言const、static和extern的用法

    <OOC>笔记(1)——C语言const.static和extern的用法 C语言中const关键字用法不少,我只喜欢两种用法.一是用于修饰函数形参,二是用于修饰全局变量和局部变量. 用c ...

  3. extern "C" 用法解析

    extern "c"用法解析 作者 作者Jason Ding ,链接http://www.jianshu.com/p/5d2eeeb93590 引言 C++保留了一部分过程式语言的 ...

  4. 变量的声明和定义以及extern的用法

    变量的声明和定义以及extern的用法                                          变量的声明不同于变量的定义,这一点往往容易让人混淆. l         变量 ...

  5. 关于extern的用法

    extern表示该变量或者函数时在另一个地方定义了. 在C++编程中,如果将程序分为多个文件,则需要有在文件间共享代码的方法,这时如果一个变量或者函数需要在多个文件中使用,则可以使用extern来声明 ...

  6. static和extern的用法小结

    以前写程序是,基本不管static和extern,一个工程文件也只有一个c文件.今天尝试用多个文件来写,自然就涉及到这两个关键词的使用,自己查了些资料,并且做了些实验,总结如下. extern的用法 ...

  7. 命名空间 extern的用法 static全局变量

    std是标准库中的命名空间: 关于extern的用法可以参考文献http://blog.163.com/sunjinxia%40126/blog/static/94984879201312145021 ...

  8. ZT --- extern "C"用法详解 2010-08-21 19:14:12

    extern "C"用法详解 2010-08-21 19:14:12 分类: C/C++ 1.前言: 时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus ...

  9. C++基础--extern的用法

    extern作为外部变量扩展的用法: 1. 主要作用是扩展变量或者函数的应用范围: 2. extern的用法是相对于全局变量而言: 3. 在看到extern这个关键字的时候说明这个变量已经在别的源文件 ...

随机推荐

  1. Atitit 代理CGLIB 动态代理 AspectJ静态代理区别

    Atitit 代理CGLIB 动态代理 AspectJ静态代理区别 1.1. AOP 代理主要分为静态代理和动态代理两大类,静态代理以 AspectJ 为代表:而动态代理则以 spring AOP 为 ...

  2. golang开发缓存组件

    代码地址github:cache 花了一天时间看了下实验楼的cache组件,使用golang编写的,收获还是蛮多的,缓存组件的设计其实挺简单的,主要思路或者设计点如下: 全局struct对象:用来做缓 ...

  3. PHP实现RESTful风格的API实例(二)

    接前一篇PHP实现RESTful风格的API实例(一) Response.php :包含一个Request类,即输出类.根据接收到的Content-Type,将Request类返回的数组拼接成对应的格 ...

  4. Jasmine入门(结合示例讲解)

    参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...

  5. javaweb+SSH实现简单的权限管理系统

    权限管理,平时里很多地方我们都可以看到,比如聊QQ时群里的群主.管理员以及成员之间的功能是不一样的--大家一定会遇到的一个问题,所以整理 一下自己写权限系统的一些经验给大家,只起参考作用,也望大家笑纳 ...

  6. JS中的匿名函数

    整理自:http://www.cnblogs.com/playerlife/archive/2012/10/17/2727683.html 一.什么是匿名函数? 在Javascript定义一个函数一般 ...

  7. KendoUI系列:TabStrip

    <link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css")" ...

  8. Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)

    1.启动glassfish服务器, 在浏览器的地址栏中输入 http://localhost:4848 2.首先建立JDBC Connection Pools: 3.new 一个Connectio P ...

  9. bootstrap-table 分页的问题

    文档网站 http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#via-javascript-table 问题1 :服务器端取不到f ...

  10. Request.UrlReferrer 使用

    最近有一个功能是反馈统计,然后在反馈建议里面添加是从哪个页面点击过来的,一开始打算做成&url=这种方法加在链接里面然后页面接受参数,后来知道了request.UrlReferrer 知道他可 ...