Thinking in c++ p98
//enum.c
#include <stdio.h>
enum ShapeType{
circle = 10, square = 20, rectangle = 30
};
int main(){
enum ShapeType shape = circle;
shape++;
printf("%d\n", shape);
return 0;
}
amrzs@ubuntu:cc$ gcc enum.c
amrzs@ubuntu:cc$ ./a.out
11
//enum.cpp
#include <cstdio>
using namespace std;
enum ShapeType{
circle = 10, square = 20, rectangle = 30
};
int main(){
ShapeType shape = circle;
shape++;
printf("%d\n", shape);
return 0;
}
amrzs@ubuntu:cc$ g++ enum.cpp
enum.cpp: In function ‘int main()’:
enum.cpp:11:10: error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]
shape++;
这点可以看出,C++并不是C的超集,把C++看做另外一门语言更好,因为C++与C有很多兼容的语法,使得C程序员更容易入手C++。
但总的来说,写法标准的C程序还是可以用C++编译的,只是C++的编译器语法检查更加严格。