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

8次阅读

共计 2652 个字符,预计需要花费 7 分钟才能阅读完成。

C 与 C++ 的关系

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

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.cppTest_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 = %d\n”, c);

register int a = 0;

printf(“&a = %p\n”, &a);

printf(“End …\n”);

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

gcc 输出:
test.c:9: error:‘for’loop 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:‘for’loop initial declarations are only allowed in C99 mode
test.c:21: error: address of register variable‘a’requested

Test_2.cppTest_2.c
#include <stdio.h>

int g_v;
int g_v;

int main(int argc, char* argv[])
{
printf(“g_v = %d\n”, 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(“%d\n”, i);
}

g()
{
return 5;
}
问题:

函数 f 的返回值和参数分别是什么类型?
函数 g 可以接受多少个参数?

在 C 语言中

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

在 C++ 中

int f() 和 int f(void) 具有相同的意义
表示返回值为 int 的无参函数

实例分析:C 到 C++ 的升级
Test_3.cppTest_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.cppTest_4.c
#include <stdio.h>

f(i)
{
printf(“%d\n”, i);
}

g()
{
return 5;
}

int main(int argc, char* argv[])
{
f(10);

printf(“g() = %d\n”, 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 function‘int main(int, char**)’:
test.c:15: error:‘f’was not declared in this scope
test.c:8: error: too many arguments to function‘int g()’
test.c:17: error: at this point in file

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

小结

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

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

正文完
 0