zoukankan      html  css  js  c++  java
  • C++_class_powerpoint_1.2

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

    Expressions and Statements

    Operator summary

    • Scope resolution   class::member

               namespace::member

    • Global                   ::name                                

               ::qualified-name

     1 namespace NS { int x; }
     2   int x;
     3  void f()
     4  {
     5     int x=1;
     6     ::x=2;
     7     ::NS::x=3; 
     8     x=4;
     9 10  }

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

    • 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返回的指针。
    1 int* pi = new int{5};
    2              // using by expression *pi
    3              delete pi;

    又如

     1 char* save_string(const char* p)
     2 {   char* s= new char[strlen(p)+1]{};
     3     strcpy(s,p);
     4     return s;
     5 }
     6 int main(int argc,char** argv)
     7 {   char* p=save_string(argv[1]);
     8  9     delete[] p;
    10 }
    1 void f(int n)
    2 {
    3    vector<int>* p = new vector<int>(n);
    4    int* q = new int[n]{2,6};
    5 6    delete p;
    7    delete[] q;
    8 }

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

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


    Type cast

    1 void* malloc(size_t);
    2  void f()
    3  {
    4   int* p =
    5        static_cast<int*>(malloc(100));
    6 7  }

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

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

    1 void f()
    2 {
    3  IO_device* p =
    4   reinterpret_cast<IO_device*>(0xff00);
    5 6 }
    7 // same bit pattern, different interpretations

    const_cast:

    1 void f()
    2 { const int i=100;
    3   const int* p = &i;
    4   int* q = const_cast<int*>(p);
    5 6 }
    7 // removing const modifier


    Constructors

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

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

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

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

    Declaration statements

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

    1 if (double d=prim(true)) 
    2 {
    3     left /= d;
    4     break;
    5 }

     

    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&:  可以传递字面量,常量,或由类型转换生成的对象。
    1 float fsqrt(const float&);
    2  double d;
    3  float r = fsqrt(2.0f);//t.o.
    4  r = fsqrt(r);
    5  r = fsqrt(d); // t.o.
    1  float fsqrt(float&);
    2  double d;
    3  float r = fsqrt(2.0f); //ERROR!!
    4  r = fsqrt(r);
    5  r = fsqrt(d); // ERROR!
    6  


    Value return

    • 函数返回的语义是初始化(不是赋值)。
    • 返回语句被认为初始化函数类型的一个临时对象。
    • 不要返回指针或本地变量的引用!
    • 经常使用“move”语义而不是“copy”
     1 float fsqrt(const float& r)
     2  { float x;
     3  4    return x;    // float temp=x;
     5  } 
     6  float d;
     7  float r= fsqrt(d); 
     8   // r=temp, then temp is destroyed.
     9   // temp may be optimized away 
    10   // 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.

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

    1 void print(double);
    2  void print(long);
    3 
    4  print(1L);  // print(long)
    5  print(1.0); // print(double)
    6  print(1);   
    7   // standard conversion : intdouble, intlong
    8  // ambiguous: print(long(1)) or print(double(1))?
    9  // solution: [see Ambiguity Resolution Slide later]
    1 void print(double);
    2  void print(long);
    3  print(1);   
    4 5   print(static_cast<double>(1));   
    6 //Or
    7   print(static_cast<long>(1));   

    多参数时,为每一个参数找到最佳的匹配,一个函数的一个参数要是最佳匹配,其余参数只有都一样匹配或者更匹配,才会调用成功(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)。

    1 int    pow(int   ,int   ); 
    2  double pow(double,double);
    3 
    4  double d = pow (2.0 , 2);
    5 //best match for arg1 is the second
    6 //best match for arg2 is the first
    7 // ambiguous!

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

    1 void f(int);
    2  void g()
    3  { 
    4     void f(double);
    5     f(1);   // f(double)
    6  }

    Default arguments

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

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

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

    1  int g(int);
    2  void f(int = g(5)); 
    3  // type checking
    4 
    5  f();
    6  // default value computing 

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

    1 void f1(int, int=0, char* =0);
    2 
    3 void f2(int, int=0, char* );
    4   // Error!
    5 void f3(int=0, int, char* =0);    
    6   // Error!

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

    1 void f(int = 7);
    2 void f(int);
    3 void f(int = 7); // error!
    4 void f(int = 8); // error!
    5 void g()
    6 {  void A::f(int x = 9); 
    7    // another function
    8 9 }
  • 相关阅读:
    UI进阶--控制器View的创建流程以及生命周期方法
    UI进阶--程序启动原理和UIApplication
    UI进阶--常见的项目文件介绍
    浅析 KVO 内部实现
    浅析 Get 和 Post 的区别
    C++实现进制转换
    UIBlurEffect 毛玻璃特效实现
    UITableView左滑显示选择
    排序算法(OC实现)
    valid Palindrome -- leetcode
  • 原文地址:https://www.cnblogs.com/Jiiiin/p/8662729.html
Copyright © 2011-2022 走看看