首页 > 其他 > 详细

关于extern的使用

时间:2015-04-05 18:52:41      阅读:162      评论:0      收藏:0      [点我收藏+]

1.同一个文件中

#include<iostream>

using namespace std;

void main()

{

extern int x;

cout<<x<<endl;

}

int x=2;


2.多文件使用(只要使用外部变量的文件和定义外部变量的文件在同一个工程即可)


func.cpp

#include "func.h"
#include <iostream>
int x=0;


int func()
{
    return x++;
}



func.h

int func();

main.cpp

#include <iostream>
#include "func.h"
using namespace std;


extern int x;


int main()
{
    cout << "Hello world!" << endl;


    cout<<"x: "<<x<<endl;




    cout<<"func x: "<<func()<<endl;
    cout<<"x : "<<x<<endl;
    return 0;
}

技术分享


关于extern的使用

原文:http://blog.csdn.net/wy7980/article/details/44889567

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!