-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
## Effective C++ | ||
|
||
1. 视 C++ 为一个语言联邦(C、Object-Oriented C++、Template C++、STL) | ||
2. 宁可以编译器替换预处理器(尽量以 const、enum、inline 替换 #define) | ||
2. 宁可以编译器替换预处理器(尽量以 const、enum、inline 替换 #define) | ||
3. 尽可能使用 const | ||
4. 确定对象被使用前已先被初始化(构造时赋值(copy 构造函数)比 default 构造后赋值(copy assignment)效率高) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 尽量使用const enum inline代替#define | ||
|
||
// 1. 使用const代替可以保留符号信息 报错时候可以定位到具体的符号 否则只能看到1.635 | ||
#define ASPECT_RATION 1.653 | ||
const double AspectRatio = 1.653; | ||
|
||
// 2.class专属常量 | ||
// 类中的静态成员 | ||
class GamePlayer { | ||
public: | ||
static int getN() { | ||
return NumTurns; | ||
} | ||
private: | ||
// 常量声明式 如果没有声明初值 一定要有21行的定义式 一定要指定初值 | ||
// const int GamePlayer::NumTurns = 0; // 位于实现文件中 | ||
// static const int NumTurns; | ||
// 常量声明式 只声明但是没有定义 没有分配内存 实际上在c++11中不用再定义 | ||
static const int NumTurns = 5; | ||
// int scores[NumTurns]; | ||
}; | ||
const int GamePlayer::NumTurns; // NumTurns定义 分配内存 | ||
|
||
class CostEstimate { | ||
private: | ||
static const double FudgeFactor; // 常量声明位于头文件中 | ||
}; | ||
const double CostEstimate::FudgeFactor=0; // static class 常量定义 位于实现文件中 | ||
|
||
#include <iostream> | ||
using std::cout; | ||
using std::endl; | ||
int main() { | ||
// 有访问权限 需要一个静态函数访问 | ||
cout << GamePlayer::getN() << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
class TextBlock { | ||
public: | ||
TextBlock(const char* t){ | ||
if(t) { | ||
text = new char[strlen(t)]; | ||
strcpy(text, t); | ||
} else { | ||
text = new char[1]; | ||
text[0] = '\0'; | ||
} | ||
} | ||
~TextBlock() { | ||
delete []text; | ||
} | ||
const char& operator[](std::size_t idx) const { | ||
return text[idx]; | ||
} | ||
// 为了避免重复代码 可以通过类型转换的模式实现非const的操作 | ||
char& operator[](std::size_t idx) { | ||
// 然后返回的结果是const char& 只能通过const_cast转为非const | ||
return const_cast<char&> ( | ||
// 先将对象强制转换为const类型 使用static_cast | ||
static_cast<const TextBlock&>(*this)[idx] | ||
); | ||
} | ||
private: | ||
char* text; | ||
}; | ||
|
||
int main() { | ||
TextBlock tb("text"); | ||
const TextBlock ctb("hello"); | ||
std::cout << tb[1] << std::endl; | ||
std::cout << ctb[0] << std::endl; | ||
} |