zoukankan      html  css  js  c++  java
  • C与C++不同

    常量表示方法不同

    C不支持引用,C++支持

    注释不同,C89不支持单行注释

    (++i)++在C中不合法

    (a=3)=4在C中不合法

    不能在for循环头部定义变量

    C++注重类型,强类型,严格检查类型

    C类型检查不明确

    //在C可以编译,在C++无法编译

    //1>main.cpp(10): error C2440: “=”: 无法从“double *”转换为“int *”

    //1> main.cpp(10): note: 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 void main()
     5 {
     6     int *p1 = NULL;
     7 
     8     double *p2 = NULL;
     9 
    10     p1 = p2;//在C可以编译,在C++无法编译
    11 
    12     //1>main.cpp(10) : error C2440 : “ = ” : 无法从“double *”转换为“int *”
    13     //    1>  main.cpp(10) : note : 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
    14 
    15     system("pause");
    16 }

    适用于宽字符串

    wchar_t

    std::wcout

     1 #include <iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     char *str("china");//字符串
     7     wchar_t *str1(L"china");//宽字符串
     8 
     9     std::cout << str << std::endl;
    10     std::wcout << str1 << std::endl;
    11 
    12     system("pause");
    13 }

    //C++检测到右值在内存有实体,自动转换为左值

    //C不会把右值转换为左值

    (a = 3) = 4;

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //C++检测到右值在内存有实体,自动转换为左值
     5 
     6 void main()
     7 {
     8     int a = 3;
     9     
    10     std::cout << a << std::endl;//3
    11 
    12     (a = 3) = 4;
    13 
    14     std::cout << a << std::endl;//4
    15     
    16     system("pause");
    17 }

    ((a > b) ? a : b) = 2;

     1 #include <iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     int a = 3;
     7     int b = 5;
     8 
     9     std::cout << a << " " << b << std::endl;//3 5
    10 
    11     ((a > b) ? a : b) = 2;
    12 
    13     std::cout << a << " " << b << std::endl;//3 2
    14     
    15     system("pause");
    16 }

    (++a)++;

     1 #include <iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     int a = 3;
     7     
     8     std::cout << a << std::endl;//3
     9 
    10     (++a)++;
    11 
    12     std::cout << a << std::endl;//5
    13     
    14     system("pause");
    15 }

    //C全局变量有声明与定义的差别
    //C++全局变量没有声明与定义的差别

    //error C2086: “int a”: 重定义

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //C全局变量有声明与定义的差别
     5 //C++全局变量没有声明与定义的差别
     6 
     7 int a;//error C2086: “int a”: 重定义
     8 int a;
     9 int a;
    10 int a;
    11 
    12 void main()
    13 {
    14     
    15     system("pause");
    16 }

    //C静态全局变量有声明与定义的差别
    //C++静态全局变量没有声明与定义的差别

    //error C2086: “int a”: 重定义

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //C静态全局变量有声明与定义的差别
     5 //C++静态全局变量没有声明与定义的差别
     6 
     7 static int a;//error C2086: “int a”: 重定义
     8 static int a;
     9 static int a;
    10 static int a;
    11 
    12 void main()
    13 {
    14     
    15     system("pause");
    16 }

    //register在C++编译器做了优化,如果检测到取地址,就不会把它放到寄存器

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //register在C++编译器做了优化,如果检测到取地址,就不会把它放到寄存器
     5 
     6 void main()
     7 {
     8     register int num(998);
     9 
    10     std::cout << &num << std::endl;
    11     
    12     system("pause");
    13 }

    //C++编译宽泛
    //为了修改源代码,后面留下拓展
    //留空,用于占位

    void test(int a, double, int)
    {
    std::cout << a << std::endl;
    }

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //C++编译宽泛
     5 //为了修改源代码,后面留下拓展
     6 //留空,用于占位
     7 
     8 void test(int a, double, int)
     9 {
    10     std::cout << a << std::endl;
    11 }
    12 
    13 void main()
    14 {
    15     test(1, 2.9, 3);//1
    16     
    17     system("pause");
    18 }
  • 相关阅读:
    linux下使用 du查看某个文件或目录占用磁盘空间的大小
    Mac 使用bootcamp安装windows 运行Hyper-v时的硬件虚拟化没有启动的问题
    DockerToolbox安装docker
    如何在Ubuntu中安装Docker和运行 Docker容器
    react native开发过程遇到的各种问题汇总
    程序员接私活怎样防止做完了不给钱?
    Creat React App deploy on GitHub
    git 提交报错 cannot do a partial commit during a merge
    排序合并连接(sort merge join)的原理
    开源 免费 java CMS
  • 原文地址:https://www.cnblogs.com/denggelin/p/5642198.html
Copyright © 2011-2022 走看看