[英]what actually it means when we say “initialize the object” in c++?
I have tried to research it.. but I think everything is an object in c++.... like (int, float) are scalar objects..etc. 我试图研究它..但是我认为一切都是c ++中的一个对象....就像(int,float)是标量对象..etc。
But when we create class's instance, documentation refers the "initialize an object with constructor" in c++. 但是,当我们创建类的实例时,文档将引用c ++中的“使用构造函数初始化对象”。 What does it actually means.
实际上是什么意思。
I think everything is an object in c++
我认为一切都是C ++中的对象
By the letter of the standard, an "object" is "a region of storage". 按照标准的字母,“对象”是“存储区域”。 You can read the nitty-gritty details under
[intro.object]
. 您可以在
[intro.object]
下阅读详细内容。
But in layman's terms yes you are right. 但是,用外行的话来说,你是对的。
like (int, float) are scalar objects..etc.
像(int,float)是标量对象..etc。
Absolutely. 绝对。 An
int
is an object. 一个
int
是一个对象。 A float
is an object. float
是一个对象。
(Of course, int
and float
themselves are types .) (当然,
int
和float
本身是类型 。)
But when we create class's instance, documentation refers the "initialize an object with constructor" in c++.
但是,当我们创建类的实例时,文档将引用c ++中的“使用构造函数初始化对象”。
There's nothing wrong with that. 没有错。 You can initialise an
int
, and you can initialise a float
, and you can initialise an object of class type. 您可以初始化一个
int
,可以初始化一个float
,并且可以初始化一个类类型的对象。 For the latter case, one way to do that is using a constructor. 对于后一种情况,一种方法是使用构造函数。 That doesn't change anything.
那没有任何改变。
What does it actually means.
实际上是什么意思。
Exactly what it says: performing the steps needed to give some object an initial value. 确切地说,它是:执行为某个对象提供初始值所需的步骤。
I'll caution you also that there is a lot of bad "documentation" (notably poor tutorials etc.) for C++ out there on the web, so it's also possible that you came across badly-worded or flat-out incorrect text. 我还要提醒您,网络上存在很多针对C ++的不良“文档”(尤其是不良的教程等),因此您也可能遇到措辞不当或平淡的错误文本。 Notice that, even in the comments section under your question, some people got this wrong.
请注意,即使在您问题的评论部分,也有人错了。
everything is an object in c++.... like (int, float) are scalar objects
一切都是c ++中的对象。...(int,float)是标量对象
This is wrong. 错了
int
and float
are built-in types. int
和float
是内置类型。 The user can define it's own types, like for example: 用户可以定义自己的类型,例如:
struct A {};
Here, A
is a user-defined type. 在此,
A
是用户定义的类型。 It is not an object! 这不是一个对象!
An object is an instance of a type: 对象是类型的实例:
A a;
int i;
Here, a
and i
are objects of type A
and int
. 在这里,
a
和i
是A
和int
类型A
对象。 When you initialize an object of type A
, it means you instanciate class A
and initialize it by calling one of the class constructors. 当您初始化类型为
A
的对象时,这意味着您实例化了类A
,并通过调用类构造函数之一对其进行了初始化。
Another example: 另一个例子:
std::vector<int> v {1,2,3}
Here, the object v
of type std::vector<int>
is initialized with the values {1,2,3}
, by calling the constructor of the class std::vector<int>
. 在这里,类型
std::vector<int>
的对象v
通过调用类std::vector<int>
的构造函数,以值{1,2,3}
初始化。
Creating an object in C++ has 2 steps: 用C ++创建对象有两个步骤:
1) find some memory to contain the object. 1)找到一些包含对象的内存。
This can be some space in the stack frame of the function for local objects or the data section for global objects. 对于本地对象,该函数的堆栈框架中可以有一些空间;对于全局对象,这可以是数据节中的一些空间。 In those cases the compiler deals with that.
在这种情况下,编译器会处理该问题。 Or
operator new
is used to dynamically create an object and allocates some memory. 或
operator new
用于动态创建对象并分配一些内存。 Which is the case if you write "new Foo()" anywhere. 如果您在任何地方编写“ new Foo()”,都是这种情况。
2) "initialize an object with constructor" 2)“使用构造函数初始化对象”
This simply means the constructor is called. 这只是意味着构造函数被调用。 The address from step 1 is passed as
this
to the constructor and any arguments you specified too. 从步骤1中的地址作为传递
this
给构造函数和任何你指定的参数太多。 If you have no constructor then the default constructor is used when possible. 如果没有构造函数,则在可能的情况下使用默认构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.