Item 1:  Prefer const and inline to #define
I--- why? const advantage?
1. because #define is not part of language prese; eg #define ASPECT_RATIO 3.1345  ,ASPECT_RATIO will be removed by the processor before entered into compiler.
2. about const trick
NOTE: const char * const myName ="shuzjd";
when we need the value of a class constant during  compilation of the class. Using enum hack technique.This  take advantage of the fact that the value of an enumerated type can be used where ints are expected.
class GamePlayer{
private:
enum {NUM_PLAYER =6};
int scores[NUM_PLSYER];}
II---using inlinde rather than #define
eg:   #define max(a,b)  ( (a)>(b)? (a):(b))
1.           inline int max(int a,int b){ return a>b ? a: b;}
2.    eg1. is not quite the same as the macro above,because this version of max can only be called with ints,but a template fixes thet problem quite nicely:
template<class T>
inline const T &max(const T& a,const T& b){ return a>b ? a: b;}
Given the availability of const and inlines, the need for preprocessor is reduced, but it's not completely eliminated.
Because #inclued #ifdef ...#ifndef #endif to play important roles in controlling compilation.
Item2: Prefer<iostearm> to <stdio.h>
type safety and extensibility are cornerstones of the c++ way of life.
Item3:use const whenever possible
 a. const modify variables(int or pointer)
char *str="greeeting"; //non-const data; non-const pointer
const char *str="hello"; //const data;non-const pointer;
const char* const str ="shuzjd"; const data;const pointer;
STL iterators are modeled on pointer.   
iterator likes a T* pointer;
std::vector<int>  ivec;
 I...const  std::vector<int>::iterator iter = ivec.begin() ; // like T* const;
*ivec=10;
ivec++;//error,iter is const;
 2...    std::vector<int>::const_iterator cIter=
vec.begin();
*ivec=10;
ivec++;//right,  data is const,ivec can change.
b.const modify function
         1.const used to  return a constant value; it can reduce the client errors.
            eg:
               class Rational{... };
             const  Rational operator* (const Rational& lhs,const Rational& rhs);
                       Q:why result of operator * be a const object?
A: Because if it weren't,clients would be able to make mistake like this:
                      if( a*b=c)//use const can be able to quickly find the error ,because  a*b is const that  can't be assignment.
     c. const member funcitons
             why use that? 
                        It's important to know which funtions modify an object and which may not.
Item 4: Make sure that objects are initialized before they're used.
         For non-member objects of built-in types. Need to do this manually.
                For example:
                          1. int x=0; //manual  initialization of an int.
                          
                          2.  double d;      //"initialization" by reading from
                                std::cin>>d;  //input stream.
                           
         The responsibility for initialization falls on constructors. Rule: make sure--all constructors initialize everything in object.
              But not to confuse assignment with initialization.
             example: Consider a constructor for a class representing entries in an address book
                    code:
                             class PhoneNum{...};
                             class ABEntry{
                                       private:
                                                 std::string theName;
                                                 std::string theAddress;
                                                 std::list<PhoneNum> thePhones;
                                                 int numTimesConsulted;
                                        ABEntry(const std::string&name, const std::string& address,const std::list<PhoneNum> phone);
                              }
ABEntry::ABEntry(const std::string& name, const std::string& address,const std::list<PhoneNumber>& phones){theName = name;                       // these are all assignments,theAddress = address;                 // not initializationsthePhones = phonesnumTimesConsulted = 0;}

A better way to write the ABEntry constructor is to use the member initialization list instead of assignments:

ABEntry::ABEntry(const std::string& name, const std::string& address,const std::list<PhoneNumber>& phones): theName(name),theAddress(address),                  // these are now all initializationsthePhones(phones),numTimesConsulted(0){}                                      // the ctor body is now empty

 sometimes must be used to initial. For example,data members that are const or are references
    
   the relative order of initialization of non-local static objects defined in different translation units is undefined.
  How to elimination the problem that caused by non-local static objects defined in different translation units?
               A:  Move each non-local static object into its own function.And declared static.These functions return references to the objects they contain. Non-local static objects are replaced with local static objects.
 

Here's the technique applied to both tfs and tempDir:

class FileSystem { ... };           // as beforeFileSystem& tfs()                   // this replaces the tfs object; it could be{                                   // static in the FileSystem classstatic FileSystem fs;             // define and initialize a local static objectreturn fs;                        // return a reference to it}class Directory { ... };            // as beforeDirectory::Directory( params )      // as before, except references to tfs are{                                   // now to tfs()...std::size_t disks = tfs().numDisks();...}Directory& tempDir()                // this replaces the tempDir object; it{                                   // could be static in the Directory classstatic Directory td;              // define/initialize local static objectreturn td;                        // return reference to it}

effective c++ 学习笔记之 Shifting from c to c++相关推荐

  1. Effective C++ 学习笔记 第七章:模板与泛型编程

    第一章见 Effective C++ 学习笔记 第一章:让自己习惯 C++ 第二章见 Effective C++ 学习笔记 第二章:构造.析构.赋值运算 第三章见 Effective C++ 学习笔记 ...

  2. Java:Effective java学习笔记之 考虑实现Comparable 接口

    Java 考虑实现Comparable 接口 考虑实现Comparable 接口 1.Comparable接口 2.为什么要考虑实现Comparable接口 3.compareTo 方法的通用约定 4 ...

  3. Effective C++学习笔记(Part Five:Item 26-31)

    2019独角兽企业重金招聘Python工程师标准>>>  最近终于把effectvie C++仔细的阅读了一边,很惊叹C++的威力与魅力.最近会把最近的读书心得与读书笔记记于此, ...

  4. Effective C++学习笔记(Part One:Item 1-4)

    2019独角兽企业重金招聘Python工程师标准>>> 最近终于把effectvie C++仔细的阅读了一边,很惊叹C++的威力与魅力.最近会把最近的读书心得与读书笔记记于此,必备查 ...

  5. 【Effective C++ 学习笔记】

    条款02:尽量以const,enum,inline替换 #define #define定义的常量也许从未被编译器看见,也许在编译器开始处理源码之前它就被预处理器移走了: #define不重视作用域,不 ...

  6. Effective C++ 学习笔记

    基于此文档 http://wenku.baidu.com/view/ef989106e87101f69e3195db.html 条款13:以对象管理资源 目标:为确保资源被释放 1.获得资源后立即放进 ...

  7. Effective C++学习笔记——构造/析构/拷贝运算

    条款9:决不再构造和析构过程中调用virtual函数,包括通过函数间接调用virtual函数. 应用:想在一个继承体系中,一个derived class被创建时,某个调用(例如生成相应的日志log)会 ...

  8. Effective C# 学习笔记(八)多用query语法,少用循环

    对于C#的query查询语法,其可读性和可维护性要比原来的loop操作好得多 例子: 同样是创建个二维元组,并对其元组按到原点的距离进行倒序排序,用query 语法的表达形式要比原始的循环做法来的更易 ...

  9. Effective Java 学习笔记 1

    Item 1: Consider static factory methods instead of constructors  (多考虑使用静态工厂方法而不是构造方法) 使用静态工厂方法有以下几点好 ...

最新文章

  1. 2008 R2 AD通过组策略针对用户进行限制QQ等软件的运行
  2. 前阿里资深运营王殿进:SaaS产品经理所面临的苦恼
  3. mysql 主键 下一个值_INNODB自增主键的一些问题 vs mysql获得自增字段下一个值
  4. VS C++/ClI调用C++ 外部Dll无法查看变量值
  5. BZOJ2440 [中山市选2011]完全平方数
  6. 卸载sqlserver
  7. 在linux下安装iNode校园客户端
  8. 【读书笔记】深入浅出数据分析
  9. Unity 渲染管线总结
  10. Linux下Socket通信中非阻塞connect、select、recv 和 recvfrom、send和sendto大致讲解,附带非租塞connect代码、MSG_NOSIGNAL
  11. 磁敏感定量成像技术的基本方法和临床应用
  12. YQP36预加水盘式成球机设计(论文+DWG图纸)
  13. CATIA CAA二次开发专题(四)------创建自己的Addin
  14. PTA 古风排版 (20 分)含测试点
  15. 图片识别商品接口 API:天猫淘宝
  16. WiFi碰碰贴开发方案
  17. java性别分类汇总,excel表格 男女数据分开-Excel按性别进行分类汇总
  18. 在龙芯1C单片机上使用ESP8266 wifi透传模块
  19. win10家庭版新增策略组及关闭密码策略
  20. 高精度人员定位系统适用于哪些行业领域?

热门文章

  1. 彻底关闭苹果系统更新_彻底关闭iPhone自动更新系统 亲测有效
  2. linux jnlp显示异常,使用headless jnlp将slave连接到master时显示异常
  3. 一个简单的姓名拼音匹配
  4. PyTorch读取自己的本地图片数据集训练自编码器
  5. Latex参考文献插入
  6. java mycat reload_MyCAT 1.6 使用reload @@config_all报错
  7. 每日一题 笨拙的手指
  8. 表达式计算器 java代码_Java实现简单的表达式计算器功能示例
  9. uni-app开发h5 发布后背景图片找不到路径
  10. 六面体体积求解(规则不规则)