码迷,mamicode.com
首页 > 编程语言 > 详细

〈Effective C++〉读书笔记--Accustoming Youself to C++

时间:2015-04-06 23:10:28      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

1、View C++ as a federation of languages。把C++看成4种子语言组成,即C、Object-Oriented C++、Template C++、The STL。

2、Things to Remember:Rules for effective C++ programming vary, depending on the part of C++ you are using.  

因为C++有很多的编程范式,在项目开发过程中,明确规范怎么使用C++很重要,这样可以使整个团队尽量使用一样的风格,并把项目一直做下去

 

3、Prefer consts, enums, and inlines to #defines。prefer the compiler to the preprocessor。因为宏定义不会被编译到符号表里面去,因此会对问题的调试定位带来麻烦,同时宏定义不能进行面向对象的封装和私有化。

 

4、使用常量代替宏定义的时候需要注意两个点,

一个是定义指针常量的时候需要使用两个const,如

 1 const char * const authorName = "Scott Meyers"; 

其中前一个const修饰char,后一个const修饰指针,可以逆着读authorName 是一个const指针,指向const的char常量

另一个是使用class-specific constant的时候,为了保持一个内存copy,需要使用static进行定义

1 class GamePlayer {
2 private:
3 static const int NumTurns = 5; // constant declaration
4 int scores[NumTurns]; // use of constant
5 ...
6 };

What you see above is a declaration for NumTurns, not a definition. Usually, C++ requires that you provide a definition for anything you use, but class-specific constants that are static and of integral type (e.g., integers, char s, bool s) are an exception. As long as you don‘t take their address, you can declare them and use them without providing a definition. If you do take the address of a class constant, or if your compiler incorrectly insists on a definition even if you don‘t take the address, you provide a separate definition like this:

 1 const int GamePlayer::NumTurns; // definition of NumTurns; see 2 // below for why no value is given 

Because the initial value of class constants is provided where the constant is declared (e.g., NumTurns is initialized to 5 when it is declared), no initial value is permitted at the point of definition.(也可以声明的时候不给初值,而在定义的时候给初值)

Note, by the way, that there‘s no way to create a class-specific constant using a #define, because #define s don‘t respect scope.

 

5、the enum hack

1 class GamePlayer {
2 private:
3 enum { NumTurns = 5 }; // "the enum hack" — makes
4 // NumTurns a symbolic name for 5
5 int scores[NumTurns]; // fine
6 ...
7 };

First, the enum hack behaves in some ways more like a #define than a const does, and sometimes that‘s what you want. For example, it‘s legal to take the address of a const, but it‘s not legal to take the address of an enum, and it‘s typically not legal to take the address of a #define, either. If you don‘t want to let people get a pointer or reference to one of your integral constants, an enum is a good way to enforce that constraint.

A second reason to know about the enum hack is purely pragmatic. Lots of code employs it, so you need to recognize it when you see it. In fact, the enum hack is a fundamental technique of template metaprogramming

 

6、another common (mis)use of the #define directive is using it to implement macros that look like functions but that don‘t incur the overhead of a function call.

 1 // call f with the maximum of a and b 2 #define CALL_WITH_MAX(a, b) f((a) > (b) ? (a) : (b)) 

替换为

1 template<typename T> // because we don‘t
2 inline void callWithMax(const T&a,const T&b)// know what T is, we
3 { // pass by reference-to-
4 f(a > b ? a : b); // const — see Item 20
5 }

 

7、Given the availability of const s, enum s, and inline s, your need for the preprocessor (especially #define) is reduced, but it‘s not eliminated. #include remains essential, and #ifdef /#ifndef continue to play important roles in controlling compilation. It‘s not yet time to retire the preprocessor, but you should definitely give it long and frequent vacations.

 

8、Things to Remember:

? For simple constants, prefer const objects or enums to #define s.
? For function-like macros, prefer inline functions to #define s.

 

9、Use const whenever possible

 

〈Effective C++〉读书笔记--Accustoming Youself to C++

标签:

原文地址:http://www.cnblogs.com/GloryLion/p/4396938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!