【C++】 2_C 到 C++ 的升级

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 2_C 到 C++ 的升级脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

C 与 C++ 的关系

  • C++ 继承了所有的 C 特性
  • C++ 在 C 的基础上提供了更多的语法和特性
  • C++ 的设计目标是运行效率与开发效率的统一

clipboard.png

C 到 C++ 的升级

  • C++ 更强调语言的实用性
  • 所有的变量都可以在需要使用时再定义
int c = 0;

for(int i=0; i<=3; i++)
{
    for(int j=0; j<=3; j++)
    {
        c += i * j;
    }
}

对比:C 语言中的变量都必须在作用域开始的位置定义 【C89】

  • register 关键字请求编译器将局部变量存储于寄存器中

    • 在 C++ 中依然支持 register 关键字
    • C++ 编译器由自己的优化方式

      • C 语言中无法获取 register 变量的地址
      • C++ 中可以取得 register 变量的地址
  • C++ 编译器发现程序中需要取 register 变量的地址时,register 对变量的声明变得无效

早期 C 语言编译器不会对代码进行优化,因此 register 变量是一个很好的补充。

  • 在 C 语言中,重复定义多个同名的全局变量是合法的
  • 在 C++ 中,不允许定义多个同名的全局变量

C 语言中多个同名的全局变量最终会被链接到全局数据区的同一个地址空间上

实例分析: C 到 C++ 的升级 一

test_1.cpp
Test_1.c

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("Begin ...n");
    
    int c = 0;
    
    for(int i=0; i<=3; i++)
    {
        for(int j=0; j<=3; j++)
        {
            c += i * j;
        }
    }
    
    printf("c = %dn", c);
    
    register int a = 0;
    
    printf("&amp;a = %pn", &a);
    
    printf("End ...n");

    return 0;
}
g++ 输出:【无警告,无错误】
Begin ...
c = 36
&a = 0xbfec9910
End ...

gcc 输出: 
test.c:9: error: ‘forloop inITial declarations are only Allowed in C99 mode
test.c:9: note: use option -std=c99 or -std=gnu99 to compile your code
test.c:11: error: ‘forloop initial declarations are only allowed in C99 mode
test.c:21: error: address of register VARiable ‘a’ requested

Test_2.cpp
Test_2.c

#include <stdio.h>

int g_v;
int g_v;

int main(int argc, char* argv[])
{
    printf("g_v = %dn", g_v);

    return 0;
}
g++输出:
test.c:4: error: redefinition of ‘int g_v’
test.c:3: error:int g_v’ previously declared here

gcc输出:
g_v = 0
  • struct 关键字的加强

    • C 语言中的 struct 定义了一组变量的集合
    • C 语言中 struct 定义的标识符并不是一种新的类型
    • C++ 中的 struct 用于定义一个全新的类型
typedef struct _tag_student Student;
struct _tag_student
{
    const char* name;
    int age;
}; 

<==> C 和 C++ 中结构体的等价定义

struct Student
{
    const char* name;
    int age;
}

面试中的小问题:int f() 与 int f(void) 有区别吗?如果有区别是什么

  • C++ 中所有的标识符都必须显示的声明类型
  • C 语言中的默认类型在 C++ 中是不合法的
f(i)
{
    printf("%dn", i);
}

g()
{
    return 5;
}

问题:

  1. 函数 f 的返回值和参数分别是什么类型?
  2. 函数 g 可以接受多少个参数?
  • 在 C 语言中

    • int f() 表示返回值为 int, 接受任意参数的函数
    • f(void) 表示返回值为 int 的无参函数
  • 在 C++ 中

    • int f() 和 int f(void) 具有相同的意义

      • 表示返回值为 int 的无参函数

实例分析: C 到 C++ 的升级

Test_3.cpp
Test_3.c

#include <stdio.h>

struct Student
{
    const char* name;
    int age;
};

int main(int argc, char* argv[])
{
    Student s1 = {"D.T.", 30};
    Student s2 = {"Software", 30};

    return 0;
}
g++输出:
无错误,无警告

gcc输出:
test.c:11: error: ‘Student’ undeclared (First use in this function)
test.c:11: error: (each undeclared identifier is reported only once
test.c:11: error: for each function it appears in.)
test.c:11: error: expected ‘;’ before ‘s1’
test.c:12: error: expected ‘;’ before ‘s2’

Test_4.cpp
Test_4.c

#include <stdio.h>

f(i)
{
    printf("%dn", i);
}

g()
{
    return 5;
}

int main(int argc, char* argv[])
{
    f(10);
    
    printf("g() = %dn", g(1, 2, 3, 4, 5));

    return 0;
}
g++输出:
test.c:3: error: expected constructor, destructor, or type conversion before ‘(’ token
test.c:8: error: ISO C++ forbids declaration of ‘g’ with no type
test.c: In functionint main(int, char**)’:
test.c:15: error: ‘f’ was not declared in this scope
test.c:8: error: too many arguments to functionint g()’
test.c:17: error: at this point in file

gcc输出:无错误,无警告
10
g() = 5

小结

  • C++ 更强调实用性,可以在任意的地方声明变量
  • C++ 中的 register 只是一个兼容作用
  • C++ 编译器能够更好的进行优化
  • C++ 中的任意标识符都必须显示的指明类型

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 2_C 到 C++ 的升级全部内容,希望文章能够帮你解决【C++】 2_C 到 C++ 的升级所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。