C与C++不同

Posted 邓戈麟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C与C++不同相关的知识,希望对你有一定的参考价值。

 

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++不同的主要内容,如果未能解决你的问题,请参考以下文章