简体   繁体   English

如果int不是类,为什么int x = int(5)合法?

[英]Why is int x = int(5) legal if int is not a class?

From what I understand it is legal to instantiate an integer in c++ like this: 根据我的理解,在c ++中实例化一个整数是合法的,如下所示:

int x = int(5);

As a Java programmer I would assume that this line of code calls the constructor of the integer passing "5" as an argument. 作为Java程序员,我假设这行代码调用传递“5”作为参数的整数的构造函数。 I read though that int is not a class and thus has no constructor. 我读过虽然int不是一个类,因此没有构造函数。

So, what does exactly happen in that line of code and what is the fundamental difference between initialising the int that way and this way: 那么,在那行代码中究竟发生了什么,以及通过这种方式初始化int之间的根本区别是什么:

int x = 5;

Thanks in advance! 提前致谢!

I read though that int is not a class and thus has no constructor. 我读过虽然int不是一个类,因此没有构造函数。

Yes, technically built-in types have no constructor . 是的,技术上内置的类型没有构造函数

what does exactly happen in that line of code 在那行代码中究竟发生了什么

The integer literal 5 is explicitly cast to an int (a no-op for the compiler mostly) and assigned to x . 整数文字5显式地转换为int (主要是编译器的no-op)并分配给x

what is the fundamental difference between initialising the int that way and int x = 5; 初始化int方式和int x = 5;之间的根本区别是什么int x = 5;

Essentially, no difference. 基本上没有区别。 All the below expressions are the same in most cases, unless you're a language lawyer (for instance, the last would prevent narrowing ie raise an error if the value cannot be represented by the type): 在大多数情况下,所有以下表达式都是相同的,除非您是语言律师(例如,最后一个会阻止缩小,即如果值不能由类型表示则引发错误):

int x = 5;         // copy initialization
int y = int(5);    // cast and copy initialization
int z = (int)5;    // cast and copy initialization
int w(5);          // direct initialization
int r{5};          // direct initialization

Read more about initializations for finer details and differences. 阅读有关初始化的更多信息,了解精细的细节和差异

C++ and java work differently when it comes to types. 当涉及到类型时,C ++和java的工作方式不同。 While java (to my understanding) uses basic built in types and reference types, C++ uses a different type system. 虽然java(据我所知)使用基本的内置类型和引用类型,但C ++使用不同类型的系统。

C++ built in types includes fundamental types (bool, character types like char, integer types, floating point types, and void) as well as some other types such as reference types like double& or std::vector<std::sting>&& and pointer types. C ++内置类型包括基本类型(bool,字符类型,如char,整数类型,浮点类型和void)以及一些其他类型,如引用类型,如double&std::vector<std::sting>&&和指针类型。

In addition to those, C++ supports user defined types (structs, classes, enum and enum class). 除此之外,C ++还支持用户定义的类型(结构,类,枚举和枚举类)。 the standard library provides many user defined types such as std::string. 标准库提供了许多用户定义的类型,例如std :: string。

it turns out the int a(5); 结果是int a(5); notation is NOT reserved for user defined types only, The ,language supports value initialization that way. 表示法不仅仅为用户定义的类型保留,该语言支持值初始化。 in C++11 it is also legal to say int a{5}; 在C ++ 11中, int a{5};int a{5};也是合法的int a{5};

now about value assignment: 现在关于价值分配:

int a; //declaration
a=5;   //assignment

int b(5); //declaration+initialization
int c=5;  //declaration+initialization (NO assignment)

If a variable has not been declared, there is no assignment, the compiler parses int c=5; 如果一个变量尚未声明,则没有赋值,编译器解析int c=5; just like int c(5); 就像int c(5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM