gpt4 book ai didi

c++ - 为什么编译器允许 string string 而不允许 int int?

转载 作者:可可西里 更新时间:2023-11-01 16:25:56 27 4
gpt4 key购买 nike

我只是想检查编译器是否允许类型名称作为变量名称。当我尝试

int int;

报错说

error C2632: 'int' followed by 'int' is illegal

但当我尝试过

#include <string>
using namespace std;

int main()
{
string string;
}

它没有给出任何错误。stringint都是数据类型。

为什么编译器允许 string 而不允许 int

编辑:包括更新。

编辑:有些人说 int 不是一个类。在那种情况下,为什么允许下行。

int a(10);

它的工作方式类似于类的构造函数。

最佳答案

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

类名和对象名相同语法上很好。

class test {}; 
int main() {
test test; // test is an object of type test.
}

关于c++ - 为什么编译器允许 string string 而不允许 int int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3601295/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com