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

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. jquery 零碎笔记

    toggle使用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  2. RTL Compiler之synthesis flow

    1 generic RTL Compiler work flow 2 invoking RTL compiler RTL Compiler is invoked from the operating ...

  3. cocos自动图集

    对于图像资源,为什么要用图集,cocos官网的解释: 1.合成图集时会去除每张图片周围的空白区域,加上可以在整体上实施各种优化算法,合成图集后可以大大减少游戏包体和内存占用2.多个 Sprite 如果 ...

  4. ARM异常中断返回的几种情况

    在学习韦老师视频中中断异常部分时候,对于发生中断时需要执行的#保存异现场 #恢复现场 中的“返回”弄不清楚,查阅网络文章后,发现一篇概述我觉得我能理解的一篇如下:   重要基础知识:R15(PC)总是 ...

  5. vue和iview中native点击事件修饰

    在父组件中给子组件绑定一个原生的事件,就将子组件变成了普通的HTML标签,不加'. native'事件是无法触 在vue中使用iview的dropdownMenu 上单纯的@click也不生效,要写成 ...

  6. MarkDown 语法及使用

    MarkDown #什么是Markdown - 定义 - markdown 是一款轻量级标记语言,功能没有HTML标记语言那么强大 ,Markdown专注书写! #试用人群: 程序员/等计算机爱好者 ...

  7. Miller Rabbin素数测试

    步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...

  8. os、sys模块

    os模块 os模块是与操作系统交互的一个接口 os.makedirs("dirname1/dirname2") # 可生成多层递归目录 os.removedirs("di ...

  9. css3 background-clip和background-origin 区别

    在css3中,background-clip和background-origin它们2个的功能大致相同,但又有些细微差别. 1.background-clip:规定背景的绘制区域,当背景是纯颜色时与图 ...

  10. hdu 5170 精度控制

    众所周知,GTY是一位神犇,为了更好的虐场,他从来不写数学作业而是去屠题,他的数学老师非常不爽,但由于GTY每次考试都AK,她也不能说什么,有一天老师在黑板上写了四个数——a,b,c,da,b,c,d ...