#define ASPECT_RATIO 1.653
涓昏鏄畯鍦ㄩ緙栬瘧鐨勬椂鍊欒縐婚櫎錛屼笉鍔犲叆緙栬瘧鍣ㄧ紪璇戯紝涓嶅埄浜庨敊璇殑媯嫻嬶紝緇欒皟璇曠淮鎶ゅ甫鏉ヤ竴瀹氬洶闅俱?br>鍥犺岀敤
const double ASPECT_RATIO = 1.653;
浠f浛銆?br>瀛樺湪2涓皬闂闇瑕佹敞鎰?
1) 鎸囬拡甯擱噺闂
const char * const authorName = "Scott Meyers";
闇瑕?嬈′嬌鐢?#8221;const”
2) 鍦ㄧ被鐨勫畾涔変腑
class GamePlayer {
private:
static const int NUM_TURNS = 5; // constant declaration
int scores[NUM_TURNS]; // use of constant

};
姝ゅ錛岃繕蹇呴』鍦?cpp鏂囦歡涓簣浠ュ畾涔?
const int GamePlayer::NUM_TURNS; // mandatory definition;
// goes in class impl. file
鍊煎緱娉ㄦ剰鐨勬槸鑰佺殑緙栬瘧鍣ㄤ笉鏀寔榪欑琛ㄨ揪鏂瑰紡錛屽洜姝よ閲囩敤濡備笅鐨勬柟寮忥細
class EngineeringConstants { // this goes in the class
private: // header file
static const double FUDGE_FACTOR;

};
// this goes in the class implementation file
const double EngineeringConstants::FUDGE_FACTOR = 1.35;
榪欑鎯呭艦涓嬪鏋滆鍦ㄧ被涓畾涔夊父閲忔暟緇勶紝闇瑕侀噰鐢ㄦ灇涓劇被鍨嬪仛涓鎶樹腑澶勭悊錛?br>
class GamePlayer {
private:
enum { NUM_TURNS = 5 }; // "the enum hack" 鈥?nbsp;makes
// NUM_TURNS a symbolic name
// for 5
int scores[NUM_TURNS]; // fine

};
閬垮厤
#define max(a,b) ((a) > (b) ? (a) : (b))
榪欑鍐欐硶銆?br>閲囩敤鍐呰繛鍑芥暟錛?br>
inline int max(int a, int b) { return a > b ? a : b; }
澧炲己閫傚簲鎬э紝搴旈噰鐢ㄦā鏉跨被錛?br>
template<class T>
inline const T& max(const T& a, const T& b)
{ return a > b ? a : b; }
鎬葷粨錛氬茍涓嶆槸璇翠笉浣跨敤瀹忥紝瑕佹槑紜湴鐭ラ亾浣跨敤瀹忓悗鍙兘浼氬紩璧風殑鍚庢灉錛屽噺灝戞湁姝т箟鐨勬儏鍐靛彂鐢熴?br>

]]>