标签:
这里说的意思其实相当于,宁可以用编译器来替换预处理器
1 class GamePlayer 2 { 3 private: 4 static const int NumTurns = 5; //常量声明式 5 int scores[NumTurns]; 6 ... 7 }
const int GamePlayer::NumTurns;
class GamePlayer { private: enum {NumTurns = 5}; int scores[NumTurns]; ... }
因为取得一个enum的地址是非法的,所以如果想禁止别人获得一个pointer或者reference指向某个整数常量,enum可以帮助实现这个约束
#define CALL_WITH_MAX(a,b) f((a) > (b) ? (a):(b))
则可以使用inline函数来进行代替,只要使用一个template inline 函数就可以了:
template<template T> inline void callWithMax(const T & a, const T & b) { f(a > b ? a : b); }
条款2:尽量以const enum inline 来替换 #define
标签:
原文地址:http://www.cnblogs.com/-wang-cheng/p/4854867.html