用英文编写(复制黏贴)果然比较吃力啊,果然还是要写中文。

Expressions and Statements

Operator summary

  • Scope resolution   class::member

           namespace::member

  • Global                   ::name

           ::qualified-name

 namespace NS { int x; }
int x;
void f()
{
int x=;
::x=;
::NS::x=;
x=;

}

在命名空间里使用同名变量,不影响。

  • Value construction   type(expression)
  • Type identification   typeid(type)
  • Type cast     dynamic_cast<T>(expression)
                           static_cast<T>(expression)
                           reinterpret_cast<T>(expression)
                           const_cast<T>(expression)
  • Create      new T
                       new T(expr-list)
                       new (expr-list) T
                       new (expr-list) T (expr-list)
  • Destroy       delete pointer
                          delete[] pointer
  • Member selection   
               object .* p2member
               pointer ->* p2member
  • Throw exception
               throw expression

Free store

  1. 命名对象(named object)的生命周期由其作用域(scope)决定,内存分配位于静态数据区域或堆栈中(data area,stack)。
  2. 在空闲存储(动态内存dynamic memory、堆heap)中创建的对象的生命周期与范围无关
  3. C中malloc和free是一个库函数,而C++的new和delete是运算符。
  4. 由new创建的对象直到被delete删除之前始终存在;delete的操作数必须是由new返回的指针。
 int* pi = new int{};
// using by expression *pi
delete pi;

又如

 char* save_string(const char* p)
{ char* s= new char[strlen(p)+]{};
strcpy(s,p);
return s;
}
int main(int argc,char** argv)
{ char* p=save_string(argv[]);

delete[] p;
}
 void f(int n)
{
vector<int>* p = new vector<int>(n);
int* q = new int[n]{,};

delete p;
delete[] q;
}

  5.当new不能找到free store时,出错:bad_alloc(声明在new中)

  6.当内存耗尽时,系统首先调用set_new_handler(在<new>中声明)。

Type cast

 void* malloc(size_t);
void f()
{
int* p =
static_cast<int*>(malloc());

}

reinterpret_cast:用于非标准的类型转换,如从一种指针到另一种,int*->char*

不能用于标准转换,如double->int

 void f()
{
IO_device* p =
reinterpret_cast<IO_device*>(0xff00);

}
// same bit pattern, different interpretations

const_cast:

 void f()
{ const int i=;
const int* p = &i;
int* q = const_cast<int*>(p);

}
// removing const modifier

Constructors

用T(e)或T{e}表示值e的T类型值的构造。

当e被省略时,T()或T{}被用来表示T类型的默认值(default value)。

似乎解释了集合栈计算机题中将set<int>()传入函数的意图。

 double d=1.3;
int i = int{d};
int j = static_cast<int>(d);
int k = int{}; //
cout<< i <<' '<<j<<' '<<k<<endl;//1 1 0

Declaration statements

在条件中声明的标识符范围扩展到条件控制语句的末尾。在这里只能声明一个id。

 if (double d=prim(true))
{
left /= d;
break;
}

Comments

不用说了

Function

Inline functions

inline int f(int n)   {return n<2 ? 1 : n * f(n-1);}

这是对编译器的一个提示(不是命令),它应该尝试为每个函数调用生成代码。函数的语义没有改变。一般来说,内联函数提高了效率,但是增加了可执行文件的长度。通常,长度只有几行代码的函数应该是内联的。否则,大型函数不应该内联。

Argument passing

参数传递的语义是初始化(不是赋值)。

这对于const参数、引用参数和一些用户定义的参数很重要。

引用传递的作用:修改传递的参数,效率(使用const T&)

  • T&:  参数必须是变量,实参的类型必须和形参一致
  • const T&:  可以传递字面量,常量,或由类型转换生成的对象。
 float fsqrt(const float&);
double d;
float r = fsqrt(2.0f);//t.o.
r = fsqrt(r);
r = fsqrt(d); // t.o.
  float fsqrt(float&);
double d;
float r = fsqrt(2.0f); //ERROR!!
r = fsqrt(r);
r = fsqrt(d); // ERROR!

Value return

  • 函数返回的语义是初始化(不是赋值)。
  • 返回语句被认为初始化函数类型的一个临时对象。
  • 不要返回指针或本地变量的引用!
  • 经常使用“move”语义而不是“copy”
 float fsqrt(const float& r)
{ float x;

return x; // float temp=x;
}
float d;
float r= fsqrt(d);
// r=temp, then temp is destroyed.
// temp may be optimized away
// by some compilers.

Overloaded functions

重载——使用相同的名称来处理不同类型的函数。

在编译时,编译器通过将实参的类型与形参的类型进行比较(而不是比较返回类型)来解决重载函数的问题。

[1] Exact match;
[2] Match using promotions;
[3] Match using standard conversions;
[4] Match using user-defined conversions;
[5] Mismatch, function undeclared.

如果找到了两个匹配项,函数调用失败。

 void print(double);
void print(long); print(1L); // print(long)
print(1.0); // print(double)
print();
// standard conversion : intdouble, intlong
// ambiguous: print(long(1)) or print(double(1))?
// solution: [see Ambiguity Resolution Slide later]
 void print(double);
void print(long);
print();

print(static_cast<double>());
//Or
print(static_cast<long>());

多参数时,为每一个参数找到最佳的匹配,一个函数的一个参数要是最佳匹配,其余参数只有都一样匹配或者更匹配,才会调用成功(A function that is best match for one argument and a better than or equal match for all other arguments is called.)不满足以上条件,调用失败(rejected as ambiguous)。

 int    pow(int   ,int   );
double pow(double,double); double d = pow (2.0 , );
//best match for arg1 is the second
//best match for arg2 is the first
// ambiguous!

在不同的非名称空间范围中声明的函数没有重载

 void f(int);
void g()
{
void f(double);
f(); // f(double)
}

Default arguments

一般的函数通常需要更多的参数来处理简单的情况。

 void print(int val, int base);
//in most cases, print in base 10 ;
// sometimes, print in base 2 , 8 , 16
void print(int val, int base = );

默认参数是在函数声明时检查的类型,并在调用时进行评估。

  int g(int);
void f(int = g());
// type checking f();
// default value computing

默认参数仅为尾随参数提供。

 void f1(int, int=, char* =);

 void f2(int, int=, char* );
// Error!
void f3(int=, int, char* =);
// Error!

在同一范围内的后续声明中不能重复或更改默认参数。

 void f(int = );
void f(int);
void f(int = ); // error!
void f(int = ); // error!
void g()
{ void A::f(int x = );
// another function

}

C++_class_powerpoint_1.2的更多相关文章

  1. C++_class_powerpoint_1.1

    Types and Declarations Boolean Type bool type – boolean , logic type bool literal – true, falseint a ...

随机推荐

  1. 【PostgreSQL-9.6.3】分区表

    PostgreSQL中的分区表是通过表继承来实现的(表继承博客http://www.cnblogs.com/NextAction/p/7366607.html).创建分区表的步骤如下: (1)创建“父 ...

  2. 用Grunt进行CSS文件压缩

    假设你的项目的CSS文件全部放在项目目录下名为css的文件夹中,现在将它压缩合并成一个名为main-min.css的文件,放在css-min文件夹下. (1)首先保证机器安装了node.js. (2) ...

  3. WING IDE 快捷键

    工欲善其事必先利其器,所以我们无论使用什么编译器,都要熟悉一些快捷键. Ctrl+N新建文件 Ctrl+O 打开文件夹 Ctrl+W 关闭当前文件 Ctrl+S 保存文件 Ctrl+shif+S 另存 ...

  4. LDAP个人理解

    在新的公司办公,所有的后台系统或文档系统都公用一个LDAP账号. 接触到这个新名词,就查了一下,谈谈个人理解: LDAP是个协议, 简单地说,可以把LDAP服务理解为一套存放你账户密码的数据库系统.市 ...

  5. 获取springbean的几种方式

    首先我说一下我遇到的问题,再项目初始化时候,spring容器初始化前要执行的操作中使用到了bean去做一些增删改查操作,这样做是不能自己使用springbean的数据源去操作的,所以需要动态获取spr ...

  6. python学习笔记--深拷贝与浅拷贝的区别

    首先我们来讲讲我们python中的可变对象和不可变对象: 可变对象:该对象指向内存中的值是可以改变的.实际上是其所指的值直接发生改变,而不是发生复制,或者开辟一个新的地址空间.例如:列表list,字典 ...

  7. 如何利用showdoc自动生成API文档

    介绍 showdoc是一个适合IT团队的文档工具,阅读本文前需要对showdoc有基本了解 .基本介绍可看:https://www.showdoc.cc/help 对于写API文档这件事,虽然说文本编 ...

  8. 7 numpy 傅里叶,提取图片轮廓

    任务:提取照片中轮廓   本次处理图片:我的女神之一  江一燕   导入模块: #jyy.show()   会打开本地图片浏览器       使用傅里叶反转 获取实部,舍弃虚部 去除小数部分 将一维数 ...

  9. CVE-2014-6271 漏洞告警

    原理:BASH除了可以将shell变量导出为环境变量,还可以将shell函数导出为环境变量!当前版本的bash通过以函数名作为环境变量名,以“(){”开头的字串作为环境变量的值来将函数定义导出为环境变 ...

  10. 【codeforces 514C】Watto and Mechanism(字符串hash)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...