C\C++区别 枚举类型enum

C\C++区别 枚举类型enum

from my csdn blog

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++可以说是ANSI C的超集,至于后来的C99又多了很多新的内容,与C++分道扬镳了
    • 写C就是写C,不要尝试使用C++编译器来实现
    • 可读性与可移植性,不要尝试那些诡异的代码,不同的编译器实现机制还不一样
    • 使用C++的方式更加安全
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容