知识库

C++语言开发示例:06. 外部变量

2025-02-01 20:32:10 李腾 1 次阅读

external.cpp

#include <iostream>

using namespace std;

double warming = 0.3;

void update(double dt);
void local();

int main()
{
    cout << "Global warming is " << warming << " degrees.\n";
    update(0.1);
    cout << "Global warming is " << warming << " degrees.\n";
    local();
    cout << "Global warming is " << warming << " degrees.\n";
}

support.cpp


#include <iostream>

extern double warming;

void update(double dt);
void local();

using std::cout;

void update(double dt)
{
    extern double warming;
    warming += dt;
    cout << "Updating global warming to " << warming;
    cout << " degrees.\n";
}

void local()
{
    double warming = 0.8;

    cout << "Local warming = " << warming << " degrees.\n";
    cout << "But global warming = " << ::warming;
    cout << " degrees.\n";
}
转载请注明转自www.hylab.cn,原文地址:C++语言开发示例:06. 外部变量

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!