Prefer const, enum ,inlines to #defines

/*
 * File:   main.cpp
 * Author: qzhao
 * 这个章节是关于在进行C++日常开发的时候,尽量使用const 或者enum 取代传统的C define 宏,
 * 宏是提供给预编译器,来进行简单的文本替换
 * 但是C++ 推荐采用const 或者 enum 变量定义. 直接让compiler 来帮忙检错。
 * just as “perfer the compiler to the preprocessor " Scott Meyyers
 * The advantages of using const instead of define
 *   The scope can be controled where marco will not
 *  Normally we must define the const value if it was declared. That's being
 *  case when we declare a const value( should be inner types : int float ...)
 * in a class definiation. it can be used without providing a defination if we
 * don't take there address(当然,如果取地址的话,时机上必须要给对象分配内存空间)
 * 如果只是简单的取值的话,编译器应该是做了简单的替换操作(这点类似预处理器)
 *
 *Things to Remember:
 *  For simple constants, prefer const objects or enums to #defines
 *  For function-like macros, prefer inline functions to #defines
 * Created on 2011年7月2日, 上午11:25
 */

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

const string ThingsToRemember = " Things to Remember:\n\
For simple constants, prefer const objects or enums to #defines\n\
For function-like macros, prefer inline functions to #defines";

const float PI = 3.1415926;
const char * const authorName = "Scott Meyyers";
const string fresher("Simon zhao");

class MyFamily {
public:

    MyFamily() {
        memberName[0] = "Father";
        memberName[1] = "Mother";
        memberName[2] = "Brother";
        memberName[3] = "Honyan";
        memberName[4] = "Saozi";
        memberName[5] = "JIaxin";
        memberName[6] = "Me";
    }
    void show() const;
    static const size_t kTotalMembers = 7; //
    static const float distance = 2000.0;
    //static const string address = "Shandong provience";
private:
    string memberName[kTotalMembers];
};

const size_t MyFamily::kTotalMembers;
//const string MyFamily::address;

void MyFamily::show() const {
    cout << "I love my family" << endl;
    for (size_t i = 0; i < kTotalMembers; ++i) {
        cout << memberName[i] << endl;    }

    cout << "We are at " << distance << endl;
}

void InlineFunc();
/*
 *
 */
int main(int argc, char** argv) {

    cout << "Prefer const enum inline to define" << endl;

    cout << "PI = " << PI << endl;

    cout << "The author of Effective C++ is: " << authorName << endl;

    cout << "And I (" << fresher << ")" << " should study from him." << endl;

    //PI = 22;

    //authorName = "New name";
    //*authorName = "naa0";
    MyFamily myFamliy;
    myFamliy.show();

    cout << "Member count " << MyFamily::kTotalMembers << endl;
    const size_t *pAddress = &MyFamily::kTotalMembers;
    cout << pAddress << endl;
    cout << "The address is :" << &MyFamily::kTotalMembers << endl;

    InlineFunc();

    cout << ThingsToRemember;

    return 0;
}


#define CALL_WIDTH_MAX(a, b)  (a) > (b) ? (a) : (b)

void InlineFunc()
{
    int a = 5;
    int b = 0;

    CALL_WIDTH_MAX(++a, b);
    CALL_WIDTH_MAX(++a, b+10);

    cout << "a should be(5, 6, 7, 8)" <<endl;

    cout << "a = " << a << "," << "b = " << b <<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值