const char* const autoName = "scott Meyers";
// 使用string 代替char* 效果更好
const std::string authorName = "scott Meyers";
实现方式一
class GamePlayer
{
public:
static const int NumTurns = 5;// 静态变量,这里是声明,不是定义
int scores[NumTurns];
};
const int GamePlayer::NumTurns;// 此处不能在设置值,否则报错,error C2374: “NumTurns”: 重定义;多次初始化
实现方式二
针对旧的编译器,可以写成下面的样子,可以写成通用的格式
class CostEstimate {
private:
static const double FudgeFactor;
public:
void print() {
cout << FudgeFactor << endl;
}
};
const double CostEstimate::FudgeFactor = 1.35;
实现方式三
class GamePlayer
{
public:
enum{
NumTurns = 6
};
int scores[NumTurns];
};
#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))
// 使用下方代替
template<typename T>
inline void callMax(const T& a, const T& b) {
f(a > b ? a : b);
}
#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))
#include
using namespace std;
class GamePlayer
{
public:
static const int NumTurns = 5;// 静态变量,这里是声明,不是定义
int scores[NumTurns];
};
const int GamePlayer::NumTurns;// 此处不能在设置值,否则报错,error C2374: “NumTurns”: 重定义;多次初始化
class CostEstimate {
private:
static const double FudgeFactor;
public:
template<typename T>
inline void callMax(const T& a, const T& b) {
f(a > b ? a : b);
}
void print() {
cout << FudgeFactor << endl;
}
};
const double CostEstimate::FudgeFactor = 1.35;
//class GamePlayer
//{
//public:
// enum {
// NumTurns = 6
// };
// int scores[NumTurns];
//};
int main()
{
std::cout << "Hello World!\n";
GamePlayer gp;
cout << gp.NumTurns << endl;
CostEstimate ce;
ce.print();
return 0;
}