• C++学习笔记之 C++对C的增强和拓展


    C++对C的增强和拓展

    增强

    1. 全局变量检测增强

    int a;
    int a = 0;
    
    int main(int argc,char *argv[])
    {
    
        return EXIT_SUCCESS;
    }
    
    D:CC++C++Day1main.cpp|5|error: redefinition of 'int a'|
    D:CC++C++Day1main.cpp|4|note: 'int a' previously declared here|
    

    该代码在C语言可以成功编译

    2. 函数检测增强

    C语言中函数返回值可以不写,写了和返回类型不符也不会检测

    形参列表中参数可以不写类型,数量可以不一样

    但在C++中不可以

    函数检测增强 形参类型检测增强

    test(w,s)
    {
        printf("%d
    ",w*s);
    }
    
    int main()
    {
        test(10,10,10);
    }
    

    上面的代码在C语言中编译通过,在C++中会报错

    3. 类型转换检测增强

    char *s = malloc(sizeof(char)*64);
    // 报错
    D:CC++C++Day1main.cpp||In function 'int main(int, char**)':|
    D:CC++C++Day1main.cpp|6|error: invalid conversion from 'void*' to 'char*' [-fpermissive]|
    D:CC++C++Day1main.cpp|6|warning: unused variable 's' [-Wunused-variable]|
    

    4. struct增强

    在C语言中结构体内不能有函数

    C++中结构体中可以有函数

    // main.cpp
    #include <iostream>
    #include <cstdlib>
    
    typedef struct
    {
        int age;
        void func()
        {
            ++age;
        }
    } test;
    
    int main(int argc,char *argv[])
    {
        test t;
        t.age = 13;
        t.func();
        std::cout << t.age << std::endl;
    
        return EXIT_SUCCESS;
    }
    
    14
    

    C++中可以省略struct

    // 例
    struct test
    {
        int a;
    };
    
    test t; // 编译通过
    

    5. 三目运算符增强

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    int main(int argc,char *argv[])
    {
        int a = 1;
        int b = 10;
    
        a > b ? a : b = 100; // 这里返回变量b(C++)而不是其值(C)
        printf("b = %d",b);
    
        return EXIT_SUCCESS;
    }
    
    b = 100
    

    C中的解决方案

    *(a > b ? &a : &b) = 100;
    

    优先级

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    int main(int argc,char *argv[])
    {
        int a = 1;
        int b = 10;
    
        a < b ? a : b = 100;
        printf("a = %d",a);
    
        return EXIT_SUCCESS;
    }
    
    a = 1
    

    在这里b = 100的优先级高

    相当于:

    if (a < b)
    {
        return a;
    }
    else
    {
        return b = 100;
    }
    

    解决:

    (a < b ? a : b) = 100;
    

    6. const增强

    • C语言中
      • 全局const int a = 10;
        • 直接修改a = 0;:失败
        • 间接修改(指针):成功
      • 局部
        • 直接修改:失败
        • 间接修改:成功
    • C++中
      • 全局
        • 直接修改:失败
        • 间接修改:失败
      • 局部
        • 直接修改:失败
        • 间接修改:失败
    // main.c
    #include <stdio.h>
    #include <stdlib.h>
    
    const int m_A = 10; // 受常量区保护,无法修改
    
    int main()
    {
        m_A = 0; // 失败
        int *p = NULL;
        p = &m_A;
        *p = 100; // 失败
    
        const int A = 0;
        A = 0; // 失败
        int *p = NULL;
        p = &A;
        *p = 100; // 成功
    
        return 0;
    }
    
    // main.cpp
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    const int m_A = 10; // 受常量区保护,无法修改
    
    int main()
    {
        m_A = 0; // 失败
        int *p = (int*)&m_A; // &m_A为新创建的临时内存空间
        *p = 100; // 失败
    
        const int A = 0;
        A = 0; // 失败
        int *p_ = (int*)&A;
        *p_ = 100; // 失败
    
        return 0;
    }
    

    拓展

    增加bool类型

    #include <iostream>
    #include <cstdlib>
    
    bool flag = true; // 0为false,非0自动转为1(true)
    
    int main(int argc,char *argv[])
    {
        std::cout << flag << std::endl;
        std::cout << sizeof(flag) << std::endl;
    
        return EXIT_SUCCESS;
    }
    
    1
    1
    

    未初始化默认为0(false)

  • 相关阅读:
    点击按钮icon input同步获取焦点
    修改vue项目中 elementUI input样式
    vue $route 和$router的区别
    vue 路由 vue-router 模式 hash history
    vue 路由跳转
    git 合并某个提交 git cherry-pick
    请求传参 有特殊符号
    前端 组件库 ,js等
    ajax请求成功,返回了数据,但是跳到了error情况
    微信-公众号-网页授权开发
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/13904458.html
Copyright © 2020-2023  润新知