c++复习-基础-从c到c++

最近要开qt项目,所以先复习一下c++,下周可能就更新qt笔记了。

参考:
https://www.runoob.com/cplusplus/cpp-tutorial.html
https://c.biancheng.net/cplus/


  • namespace std建议只在函数内部使用,不要放全局

  • cout和cin都是c++内置对象,不是关键字

  • 使用cout输出bool类型的值,结果是0或1

  • 逗号运算符:整个逗号表达式的值是以逗号分隔的列表中的最后一个表达式的值

    int i = 10;
    cout << (i,i+2) << endl; // 12
    
  • lambda函数

    • 也就是匿名函数,也称为lambda表达式;将函数看作对象,可以赋值给变量或者作为参数传递,也可以对其求值。

    • 格式:

      // 有返回值的情况,例如:[](int x, int y)->int{ int z = x + y; return z + x; }
      [capture](parameters)->return-type{
             body}
      // 无返回值的情况,例如:[]{ ++g; }
      [capture](parameters){
             body}
      

      其中,capture里是访问当前作用域的变量时的方式(值,还是引用?)

      [] 没有定义任何变量,使用未定义变量会报错
      [x, &y] x以传值的方式传入(默认),y以引用的方式传入
      [&] 任何被使用到的外部变量都隐式地以引用方式传入
      [=] 任何被使用到的外部变量都以传值的方式引用
      [&, x] x以传值方式使用,其他变量是引用
      [= &z] z是引用,其他变量是传值

      注意:对于[=]和[&],lambda可以直接使用this指针,但对于[],如果要使用this指针,必须要显式传入

      [this](){
              this->func(); }
      
    • 实例

    使用auto是让编译器自动推断函数的类型。

    auto add = [](int a, int b) -> int {
         
            return a + b;
        };
    int result = add(3, 5);
    
  • C++允许给函数传递数组,可以传递指针、或者type array[](长度可以不指定),但实际传的都是指针;同样,函数不能直接返回数组,需要返回一个指针。但是这个指针不能指向局部数组,除非定义局部变量是static变量或者动态分配数组。

    • static举例
    int* myFunction()
    {
         
       static int myArray[3] = {
         1, 2, 3};
       return myArray;
    }
    
    • 动态分配举例(需要使用new创建,并且在调用函数的代码块中使用delete释放)
    int* myFunction()
    {
         
       int* myArray = new int[3];
       myArray[0] = 1;
       myArray[1] = 2;
       myArray[2] = 3;
       return myArray;
    }
    
    int main()
    {
         
       int* result = myFunction();
       // 使用 result
    	 // 一定要释放,因为不会自动释放
       delete[] result;
       return 0;
    }
    
  • C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static变量。所以如果函数的返回值是指针,也需要用static变量

类型转换

  • 静态转换static cast

    • 强制转换,一般是类型相似的对象
    • 不进行运行时类型检查(可能导致错误)
    int i = 10;
    float f = static_cast<float>(i);
    
  • 动态转换dynamic cast

    • 通常用于将一个基类指针或引用转换为派生类指针或引用
    • 在运行时进行类型检查,如果不能转换则返回空指针或者引发异常
    class Base {
         };
    class Derived : public Base {
         };
    Base *ptr_base = new Derived;
    Derived *ptr_derived = dynamic_cast<Derived*>(ptr_base);
    
  • 常量转换const cast

    • 用于将const类型的对象转换为非const类型的对象
    • 只能转换掉const属性,不能改变对象类型
    const int i = 10;
    int& r = const_cast<int&>(i); // 将const int 转换为int
    
  • 重新解释转换reinterpret cast

    • 重新解释
    • 不进行任何类型检查,可能导致未定义的行为
    int i = 10;
    float f = reinterpret_cast<float&>(i);
    cout << f << endl; // 1.4013e-44
    

相关推荐

  1. c++复习-基础-cc++

    2024-01-21 18:28:01       34 阅读
  2. C#基础复习

    2024-01-21 18:28:01       13 阅读
  3. 基础入门学穿C++

    2024-01-21 18:28:01       22 阅读
  4. CC++1

    2024-01-21 18:28:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-21 18:28:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-21 18:28:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-21 18:28:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-21 18:28:01       20 阅读

热门阅读

  1. css动画旋转效果实现

    2024-01-21 18:28:01       37 阅读
  2. 在SpringBoot 3.2.1中使用JPA报错

    2024-01-21 18:28:01       39 阅读
  3. springboot启动后加载热点数据到Redis

    2024-01-21 18:28:01       35 阅读
  4. 代码随想录day32 贪心算法训练

    2024-01-21 18:28:01       31 阅读
  5. Go语言协程使用

    2024-01-21 18:28:01       35 阅读
  6. Talking about your company

    2024-01-21 18:28:01       27 阅读