zoukankan      html  css  js  c++  java
  • [C/C++]在头文件中使用static定义变量意味着什么

     文章出处:http://www.cnblogs.com/zplutor/

    看到有一位同学在头文件中这么写:

    1 static const wchar_t* g_str1 =2 static const wchar_t* g_str2 = …

    这种定义变量的方式我从来没有见过,而且它还能顺利通过编译,于是我很想知道编译器是如何处理这种变量定义的。

    定义全局变量时使用static,意味着该变量的作用域只限于定义它的源文件中,其它源文件不能访问。既然这种定义方式出现在头文件中,那么可以很自然地推测:包含了该头文件的所有源文件中都定义了这些变量,即该头文件被包含了多少次,这些变量就定义了多少次。

    假如将上面两行代码的static去掉,编译的时候就会出现变量重定义的错误,这进一步证实了上面的推测,因为没有static的话变量的作用域是全局的,定义了两个以上的同名变量就会出现该错误。

    推测终究是推测,要真正证实这个推测还要通过写代码来验证。验证的方式是:在头文件中使用static定义变量,在多个源文件中包含该头文件,然后在每个源文件中输出变量的地址,同时在一个源文件中改变变量的值并输出,在另一个源文件中也输出。如果每个源文件的输出都不同,则推测得证;否则推测是错误的。

    下面是定义变量的头文件的代码:

    1 //Header.h
    2 #pragma once
    3 
    4 static int g_int = 3;

    接下来在另一个头文件中声明两个测试函数:

    1 //Functions.h
    2 #pragma once
    3 
    4 void TestSource1();
    5 void TestSource2();

    分别在两个源文件中定义这两个测试函数:

     1 //Source1.cpp
     2 #include <stdio.h>
     3 #include "Header.h"
     4 
     5 void TestSource1() {
     6 
     7     wprintf(L"g_int's address in Source1.cpp: %08x
    ", &g_int);
     8     g_int = 5;
     9     wprintf(L"g_int's value in Source1.cpp: %d
    ", g_int);
    10 }
    1 //Source2.cpp
    2 #include <stdio.h>
    3 #include "Header.h"
    4 
    5 void TestSource2() {
    6 
    7     wprintf(L"g_int's address in Source2.cpp: %08x
    ", &g_int);
    8     wprintf(L"g_int's value in Source2.cpp: %d
    ", g_int);
    9 }

    最后在main函数中调用这两个测试函数:

    1 //Main.cpp
    2 #include "Functions.h"
    3 
    4 int wmain() {
    5 
    6     TestSource1();
    7     TestSource2();
    8 }

    运行该程序:

     可以看到,虽然在代码中好像使用了相同的变量,但是实际上使用的是不同的变量,在每个源文件中都有单独的变量。所以,在头文件中定义static变量会造成变量多次定义,造成内存空间的浪费,而且也不是真正的全局变量。应该避免使用这种定义方式。

    作为对比,下面使用正确的方式来定义全局变量:

    1 //Header.h
    2 #pragma once
    3 
    4 extern int g_int;
     1 //Source1.cpp
     2 #include <stdio.h>
     3 #include "Header.h"
     4 
     5 int g_int = 3;
     6 
     7 void TestSource1() {
     8 
     9     wprintf(L"g_int's address in Source1.cpp: %08x
    ", &g_int);
    10     g_int = 5;
    11     wprintf(L"g_int's value in Source1.cpp: %d
    ", g_int);
    12 
    13 }

    其它文件不变。

    运行程序:

     

     可以看到,这次两个源文件中使用的都是同一个变量。要注意的是,使用extern声明变量时不能带有初始值,否则仍然属于变量定义,会出现变量重定义的错误。

  • 相关阅读:
    Insus Meta Utility
    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
    Insus Binary Utility
    asp.net实现文件下载功能
    Column 'Column Name' does not belong to table Table
    程序已被编译为DLL,怎样去修改程序功能
    如何在Web网站实现搜索功能
    如何把数据流转换为二进制字符串
    Asp.net更新文件夹的文件
    如何显示中文月份
  • 原文地址:https://www.cnblogs.com/computer1-2-3/p/6088388.html
Copyright © 2011-2022 走看看