简体   繁体   English

当我们在c ++中说“初始化对象”时,这实际上意味着什么?

[英]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 .) (当然, intfloat本身是类型 。)

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. intfloat是内置类型。 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 . 在这里, aiAint类型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.

相关问题 在C ++中,当我们创建对象时,构造函数初始化,但是如果我想再次重新初始化对象,那么在main中的某个点说,该怎么办? - In C++ when we create object the constructor initializes, but if I want to reinitialize the object again, say at some point in main, what to do? C++ 原子获取/释放操作的实际含义 - C++ atomic acquire / release operations what it actually means 通过赋值初始化C ++对象时会发生什么? - What happens when you initialize a C++ object by assignment? 在C ++中,“ &lt;&gt;”是什么意思? - what '< >' means in c++? 说 C++ 对象是可移动的到底是什么意思? - What exactly does it mean to say a C++ object is movable? 对象何时在 C++ 中真正被销毁? delete(ptr) 有什么作用? - When Does Object Actually Gets Destroyed in C++? What does delete(ptr) Do? 在C ++中,当我想在初始化类对象数组时通过构造函数初始化类成员时,我该怎么办? - In C++, What should I do when I want to initialize class members via constructor when I initialize a class object array? 在C ++中声明指向对象的指针实际上是什么意思? - What does it actually mean to declare a pointer to an object in C++? 这个符号“ [=]”在C ++中是什么意思? - what this symbol “[=]” means in C++? C ++使用= assignment初始化声明用户定义的类对象时的过程是什么? - C++ what's the process when declaring a user-defined class object using = assignment to initialize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM