c – 为什么string string被允许,int int不被编译器允许?

前端之家收集整理的这篇文章主要介绍了c – 为什么string string被允许,int int不被编译器允许?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只是想检查编译器是否允许类型名称作为变量名.
当我试过
int int;

它报了一个错误

error C2632: ‘int’ followed by ‘int’ is illegal

但是当我试过

#include <string>
using namespace std;

int main()
{
    string string;
}

它没有给出任何错误.
字符串和int都是数据类型.

为什么编译器允许字符串不允许int?

编辑:包含更新.

编辑:有些人说int不是一个类.
在这种情况下,为什么在线下方是允许的.

int a(10);

它类似于类的构造函数.

解决方法

字符串不是 C++ reserved word,但int是,并且保留字不能用作标识符.

并且其语法上可以使类名和对象名称相同.

class test {}; 
int main() { 
        test test; // test is an object of type test.
}
原文链接:https://www.f2er.com/c/115953.html

猜你在找的C&C++相关文章