简体   繁体   English

在 C++ 中实例化 object 的不同方法

[英]Different methods for instantiating an object in C++

What is the difference between this:这有什么区别:

Myclass *object = new Myclass();

and

Myclass object = new Myclass();

I have seen that a lot of C++ libraries like wxWidgets, OGRE etc use the first method... Why?我已经看到很多 C++ 库,如 wxWidgets、OGRE 等都使用第一种方法......为什么?

Myclass *object = new Myclass(); //object has dynamic storage duration (usually is on the heap)
Myclass object; //object has automatic storage duration (usually is on the stack)

You create objects with dynamic storage duration (usually on the heap) if you plan on using them throughout a long period of time and you create objects with automatic storage duration (usually on the stack) for a short lifetime (or scope).如果您计划在很长一段时间内使用它们并创建具有自动存储持续时间的对象(通常在堆栈上),那么您将创建具有动态存储持续时间(通常在堆上)的对象,并且生命周期很短(或范围)。

The second is wrong !第二个错误!

You may use您可以使用

MyClass object;

That will work.那可行。

Now, concerning how to choose between these two possibilities, it mainly depends on how long your object should live.现在,关于这两种可能性如何选择,主要取决于你的object应该活多久。 See there for a thorough answer.请参阅那里以获得详尽的答案。

Your first line is 100% correct.你的第一行是 100% 正确的。 Unfortunately, you can't create object with your second line in c++.不幸的是,您不能使用 c++ 中的第二行来创建 object。 There are two ways to make/create an object in c++.有两种方法可以在 c++ 中制作/创建 object。

First one is:第一个是:

MyClass myclass; // if you only need to call the default constructor    
MyClass myclass(12); // if you need to call constructor with parameters*

Second one is:第二个是:

MyClass *myclass = new MyClass();// if you only need to call the default constructor
MyClass *myclass = new MyClass(12);// if you need to call constructor with parameters

In c++ if you use the new keyword, object will be stored in heap.在 c++ 中,如果使用new关键字,object 将存储在堆中。 It's very useful if you are using this object for a long time period and if you use first method, it will be stored in stack.如果您长时间使用此 object 非常有用,如果您使用第一种方法,它将存储在堆栈中。 it can be used only short time period.它只能在很短的时间内使用。 Notice: if you use new keyword, remember it will return pointer value.注意:如果你使用new关键字,记住它会返回指针值。 You should declare name with * .您应该使用*声明名称。 If you use second method, it doesn't delete object in the heap.如果您使用第二种方法,它不会删除堆中的 object。 You must delete by yourself using delete keyword:您必须使用delete关键字自行删除:

delete myclass;

The new operator returns a pointer to the object it creates, so the expression Myclass object = new Myclass(); new运算符返回指向它创建的 object 的指针,因此表达式Myclass object = new Myclass(); is invalid.是无效的。

Other languages don't have explicit pointers like C++ so you can write statements like Myclass object = new Myclass();其他语言没有像 C++ 这样的显式指针,所以你可以编写像Myclass object = new Myclass(); , but in C++ this is simply not possible. ,但在 C++ 中这是不可能的。 The return type of new Myclass(); new Myclass(); is a pointer to a Myclass object, ie Myclass * .是指向Myclass object 的指针,即Myclass *

The first example creates a pointer to MyClass and initializes it to point to the result of the new operator.第一个示例创建一个指向 MyClass 的指针并将其初始化为指向 new 运算符的结果。

The second will likely not compile, as it is trying to create a MyClass object and assign it to a MyClass pointer.第二个可能无法编译,因为它正在尝试创建 MyClass object 并将其分配给 MyClass 指针。 This could work in the unlikely event that you have a MyClass constructor that accepts a MyClass pointer.如果您有一个接受 MyClass 指针的 MyClass 构造函数,这可能会起作用。

Your first code line is correct while second code line is incorrect.您的第一个代码行是正确的,而第二个代码行是不正确的。

Myclass object=new Myclass();  //Incorrect code

Above code is incorrect as new Myclass();上面的代码不正确,因为new Myclass(); return pointer to class and Myclass object;返回指向 class 和Myclass object 的指针; declares object of class and you are trying to assign pointer to class to the object of class, which is incorrect. declares object of class and you are trying to assign pointer to class to the object of class, which is incorrect.

Your first code line is correct.您的第一行代码是正确的。 But this declares pointer to class not the object of class.但这声明了指向 class 的指针,而不是 class 的 object。

Myclass *object = new Myclass();  //declares pointer to class.

To declare object of class you should write following code.要声明 class 的 object,您应该编写以下代码。

Myclass object;   //declares object of class Myclass

But you should note that the way of accessing class member using pointer to class and using object of class are different.但是您应该注意,使用指向 class 的指针和使用 ZA2F2ED4F8EBC2CBB14C21A29DZ40 的 object 访问 class 成员的方式是不同的。 following is code for accessing members of class.以下是访问 class 成员的代码。

pointer_to_class->member;  // accessing class member using pointer to class
object.member;             //accessing class member using object of class 

The first is correct.第一个是正确的。

The second will generally not compile.第二个一般不会编译。 And if it does compile then the class is doing some complicated things in a constructor/assignment operator.如果它确实编译了,那么 class 会在构造函数/赋值运算符中做一些复杂的事情。 And it's probably leaking memory.它可能正在泄漏 memory。

Its posible Myclass name = Myclass();它可能的 Myclass name = Myclass();

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

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