可以在.h文件中通过const声明全局的常量,范例如下:
#ifndef TEST_H
#define TEST_H
namespace test{
//整形
const int t = 5;
//数组
const int a[5][3] ={ {1,2,3}, {4,5,1}, {4,5,1}, {4,5,1}, {4,5,1}};
//结构体
struct testIn{
int m;
int n;
};
struct testOut{
int m;
struct testIn n;
};
const struct testOut b ={5,{3,4}};
}
#endif
#ifndef TEST_H
#define TEST_H
namespace test{
//整形
const int t = 5;
//数组
const int a[5][3] ={ {1,2,3}, {4,5,1}, {4,5,1}, {4,5,1}, {4,5,1}};
//结构体
struct testIn{
int m;
int n;
};
struct testOut{
int m;
struct testIn n;
};
const struct testOut b ={5,{3,4}};
}
#endif
本文介绍如何在C++中通过.h文件利用const关键字声明全局常量,包括基本类型、数组及结构体等,并提供具体示例。
2552

被折叠的 条评论
为什么被折叠?



