C++ 定義全局數組


數組怎么用,全局數組就怎么用,只是他的作用域不一樣才叫全局數組。。。
在A.h 或 A.cpp中定義char buf[10];
如果在B.cpp要用,就在其開頭中寫成 extern char buf[10];

例如,在HelpFun.h中定義 colorTable 數組:

const Vector3f colorTable[10] = 
{
    Vector3f(0.9, 0.2, 0.2),   //  1: Red
    Vector3f(0.2, 0.2, 0.9),   //  2: Blue
    Vector3f(0.9, 0.9, 0.5),   //  3: Yellow
    Vector3f(0.9, 0.5, 0.2),   //  4: Orange
    Vector3f(0.7, 0.2, 0.9),   //  5: Purple
    Vector3f(0.3, 0.8, 0.8),   //  6: Cyan
    Vector3f(0.5, 0.3, 0.2),   //  7: Brown
    Vector3f(0.9, 0.2, 0.6),   //  8: Pink
    Vector3f(0.6, 0.6, 0.7),   //  9: Gray
    Vector3f(0.9, 0.6, 0.5)    // 10:LightSalmon
};

 

在 B.cpp要使用這個數組,只需在其開頭寫上下面一句即可。

extern const Vector3f colorTable[10];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM