你所不知道的C++

2022-11-29

C++与C的不同

C++从诞生之初就号称和C是兼容的,正是这种兼容,使C++得以迅猛发展,然而也正是这种兼容,让C++背上了沉重的历史包袱。且不论其利弊,让我们来看看C++在兼容C的那部分中,与C语言有什么不同。

1. bool

在C语言中,没有bool类型,我们通常的做法是:

   1:  #ifndef FALSE
   2:  #define FALSE 0
   3:  #endif
   4:   
   5:  #ifndef TRUE
   6:  #define TRUE (!(FALSE))
   7:  #endif

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

而在C++中,事情简单了很多,bool是内置类型,true和false也是C++的关键字,我们可以直接使用。

2. 类型转换

C语言允许void *类型隐式转换为其他类型指针,而C++不允许。比如我们使用malloc申请内存,在C和C++中的写法是不一样的:

   1:  // C
   2:  char *a = malloc(10);
   3:   
   4:  // C++
   5:  char *a = (char*)malloc(10);

当然,将其他指针类型转换为void *在两种语言中都是可以的,因为它是“向上类型转换”。

3. 字符常量的大小

在C和C++中,char类型都是占用1个字节。但是字符常量在两种语言中大小却是不一样的,在C中,字符常量与int类型一样,32位机器上占用为4个字节,而C++中,字符常量与char类型一样,占用为1个字节。

   1:  // C, output:1 4
   2:  char a = 'a';
   3:  printf("%d %d\n", sizeof(a), sizeof('a'));
   4:   
   5:  // C++, output:1 1
   6:  char a = 'a';
   7:  cout<<sizeof(a)<<" "<<sizeof('a')<<endl;
4. const变量

const变量在C语言中声明时可以不初始化,它的值根据作用域而定(如果是全局变量或static变量,初始化为0,否则为垃圾值),而在C++中声明时必须被初始化,且默认仅本文件内可见。在C中,const变量不能用于数组的声明(C99中引入的可变数组除外),而在C++中,const变量可以用在数组声明中标明数组大小。

5. 函数的声明

同样的函数声明void f();在C中,调用该函数时,可以传入任意个数的参数,但是在函数中无法正常取到传入参数的值。而在C++中,这样的函数声明就是说:我不接受任何参数。

   1:  // C, output: hello
   2:  void f()
   3:  {
   4:      printf("hello\n");    
   5:  }
   6:   
   7:  int main(int argc, char *argv[])
   8:  {
   9:      f(1, 2, 3, 4);
  10:   
  11:      return 0;
  12:  }
  13:   
  14:  // C++, compile error: too many arguments to function 'void f()'
  15:  void f()
  16:  {
  17:      cout<<"hello"<<endl;    
  18:  }
  19:   
  20:  int main(int argc, char *argv[])
  21:  {
  22:      f(1, 2, 3, 4);
  23:      
  24:      return 0;
  25:  }

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

如何在C语言中声明不接受任何参数的函数呢?答案是:

   1:  // C, function f accept no arguments
   2:  void f(void)
   3:  {
   4:      printf("hello\n");    
   5:  }

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

此外,在C语言中,在调用函数的时候不一定需要函数已经声明,找不到合式的函数声明时,编译器会默认生成一个返回类型为int的函数声明,只有在链接时如果找不到函数实现才会报错;而在C++中,调用函数时要求该函数必须已经声明,否则在编译阶段就会产生错误。

6. 结构体

在C语言中,声明结构体变量时必须有struct关键字,而在C++中,struct和class是一样的,仅用结构体名称就可以声明变量。

此外,空结构体的大小在C和C++也也有差异,C语言中为0,而C++中为1。

   1:  // C, output:0 0
   2:  struct str_empty {
   3:      
   4:  };
   5:  int main(int argc, char *argv[])
   6:  {
   7:      struct str_empty s;
   8:      printf("%d %d\n", sizeof(struct str_empty), sizeof(s));
   9:   
  10:      return 0;
  11:  }
  12:   
  13:  // C++, output: 1 1
  14:  struct str_empty {
  15:      
  16:  };
  17:  int main(int argc, char *argv[])
  18:  {
  19:      str_empty s;
  20:      cout<<sizeof(str_empty)<<" "<<sizeof(s)<<endl;
  21:      
  22:      return 0;
  23:  }

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

7. 函数默认参数

在C和Java中,都没有函数默认参数,而在C++和Python中,都可以提供默认参数。

你所不知道的C++

C++一直以来就是一个庞然大物,而随着它的发展,又不断被加入新的东西,现在的C++的复杂程度实在是让人望而生畏。这里且看看C++中有哪些比较有用而又很少被人提及的功能。

1. explict
2. function-try-block
3. 重载类型转换操作符
4. 指向成员的指针

你所不知道的C++的相关教程结束。

《你所不知道的C++.doc》

下载本文的Word格式文档,以方便收藏与打印。