Come up and C++ me some time ~ (chapter 7~11)
2015-01-14 08:48
447 查看
Point 1: const与非const指针相互转换
如果数据类型本身并不是指针,则可以将cosnt数据或非const数据的地址赋给指向cosnt的指针,但只能将非const数据的地址赋给非const指针
Point 2: C++11模板类array对象
#include<iostream> #include<array> const int Seasons= 4; const std::array<std::string, Seasons>Snames= { "Spring", "Summer", "Fall", "Winter" }; //定义 void fill(std::array<std::string, Seasons> *pa);
Point 3: 函数指针
const double * f1(const double [] , int n); const double *(*p1)(const double [] , int n)= f1;
Point 4: 内联函数与宏定义
如果程序在十个地方调用同一个内联函数,则该程序会包含该函数代码的十个副本。
要使用内联函数,需满足以下三种条件之一:
1、在函数声明前加关键字inline
2、在函数定义前加关键字inline
3、在类定义中包含函数体
内联与宏:
#define SQUARE(X) ((X)*(X)) inline SQUARE(X) { return X*X; }
宏不能按值传递,SQUARE(C++)将C递增两次,而内联函数不会。
Point 5: 引用和指针的差别
1、引用是已定义变量的别名,主要用途是用作函数的形参。通过将引用变量作为参数,函数将使用原始数据而不是其副本。
int rat= 10; int &rodent= rat;
2、引用必须定义时初始化且不能初始化后被左值,不能像指针那样先声明后赋值。
3、函数返回引用时不能返回已释放内存单元的引用。
Point 6: 默认参数规则
函数原型中设置默认值
char *left(const char * str, int n= 1); int harpo(int n, int m= 4, int j= 5);
Point 7: 函数重载规则
函数重载的关键是函数的参数列表,也称为函数特征标。如果两个函数的参数数目和类型相同,同时参数的排列顺序也相同,则他们的特征标相同(与返回值无关)。C++允许定义名称相同的函数,条件是他们的特征标不同。
void print(int num); void print(char *arr); void print(double d, int width);
Point 8: 函数模板
template <typename T> void swap(T &a, T &b); template <typename T> void swap(T *a, T *b, int n); template <typename T> void swap(T &a, T &b) { //...................................... } template <typename T> void swap(T *a, T *b, int n) { //........................................ }
Point 9: 名字空间
1、可传递可嵌套
namespace myth{
using std::cout;
using std::cin;
using namespace elements; //可包含其他名字空间
}
using namespace myth;
cin.get();
2、统一编程理念
使用在名称空间中声明的变量,而不是使用外部全局变量和静态全局变量
不要在头文件中使用using编译指令,首先这种做法可能掩盖某些可用名称,其次包含头文件的顺序可能影响程序的行为。如果非要使用,也应将其放在所有#include指令之后
对于using声明,首选将其作用域设置为局部而不是全局,别忘了使用命名空间的主旨是简化大型编程项目的管理工作
Point 10: 类的构造、析构、拷贝构造函数、深拷贝、浅拷贝
构造函数:当且仅当没有定义任何构造函数时,编译器才会提供默认构造函数。为类定义了构造函数就必须为其定义提供默认构造函数。
析构函数:对象过期时将调用的函数,如果构造函数或析构函数都私有化则调用不到
拷贝构造函数:类对象之间的赋值将调用的函数,当且仅当没有定义任何拷贝构造函数时,编译器才会提供默认的拷贝构造函数,执行的就是浅拷贝
深拷贝:如果拷贝构造函数中一旦存在动态成员,浅拷贝就会出现问题。而深拷贝时对象中动态成员将动态分配空间从而避免两个变量指向同一块内存
技巧:可以声明私有拷贝构造函数来避免默认拷贝的发生,而不必定义该构造函数。如果用户尝试按值传递或函数返回该对象时将得到一个错误,从而避免按值传递或返回对象
private: //拷贝构造,只是声明 CExample(const CExample& C);
Point 11: 运算符重载
对于成员重载运算符函数,运算符表达式中形参为运算符右边的操作数
Time Time::operator + (const Time &t) const;
Time Time::operator - (const Time &t) const;
Time Time::operator * (double mult) const;
对于非成员重载运算符函数,运算符表达式左边的操作数对于运算符参数的第一个形参,右边的操作数对于第二个形参
Time operator* (double mult, const Time &t );
相关文章推荐
- Come up and C++ me some time ~ (chapter 1~6)
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter 11 Smarter Email Marketing wit
- Synchronization and Time(Chapter 2 of Concurrent Programming on Windows)
- Namespaces and Exceptions(Chapter 8 of The C++ Programming Language)
- Chapter 11: Using Menu-Based Controls--Defining menu structure and data
- Library Organization and Containers(Chapter 16 of The C++ Programming Language)
- Chapter 4– Functions and Program Structure of TCPL (Part 11)
- [Career Cup 11][subjects from 57~59] Chapter 18 Thread and locks
- Types and Declarations(Chapter 4 of The C++ Programming Language)
- Basic Input and Output(Chapter 11 of Programming Ruby)
- Reading Notes: Chapter 1. Policy-Based Class Design(Modern C++ Design By Andrei Alexandrescu)
- 《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统--7-11
- How to do run-time (or explicit) linking of C++ plug-in components and objects
- Linux Bible, 2008 Edition: Boot up to Ubuntu, Fedora, KNOPPIX, Debian, openSUSE, and 11 Other Distri
- Expressions and Statements(Chapter 6 of The C++ Programming Language)
- Real-Time 3D Terrain Engines Using C++ and DirectX 9
- Chapter 11 Mutiple Documents and Mutiple Views
- C++ templates chapter 11(Template Argument Deduction)
- Operator Overloading(Chapter 11 of The C++ Programming Language)
- Some VS2005 and VS2008 Wizards Pop Up Script Error(转)