在 C++ 中实例化对象的不同方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/677632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different methods for instantiating an object in C++
提问by RaouL
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 等都使用第一种方法......为什么?
采纳答案by Beno?t
回答by Joe Phillips
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).
如果您计划在很长一段时间内使用它们,并且您创建具有自动存储持续时间(通常在堆栈上)的短期生命周期(或范围)对象,则您创建具有动态存储持续时间(通常在堆上)的对象。
回答by Vishva Rodrigo
Your first line is 100% correct. Unfortunately, you can't create object with your second line in c++. There are two ways to make/create an object in c++.
你的第一行是 100% 正确的。不幸的是,您无法在 C++ 中使用第二行创建对象。在 C++ 中有两种创建/创建对象的方法。
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. 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. it can be used only short time period.
Notice: if you use new
keyword, remember it will return pointer value. You should declare name with *
.
If you use second method, it doesn't delete object in the heap. You must delete by yourself using delete
keyword:
在 C++ 中,如果使用new
关键字,对象将存储在堆中。如果您长时间使用此对象,并且使用第一种方法,它将存储在堆栈中,这将非常有用。它只能在很短的时间内使用。注意:如果你使用new
关键字,记住它会返回指针值。你应该用*
. 如果使用第二种方法,它不会删除堆中的对象。您必须使用delete
关键字自行删除:
delete myclass;
回答by Welbog
The new
operator returns a pointer to the object it creates, so the expression Myclass object = new Myclass();
is invalid.
所述new
操作者将指针返回到它创建的对象,所以表达式Myclass object = new Myclass();
是无效的。
Other languages don't have explicit pointers like C++ so you can write statements like Myclass object = new Myclass();
, but in C++ this is simply not possible. The return type of new Myclass();
is a pointer to a Myclass
object, i.e. Myclass *
.
其他语言没有像 C++ 这样的显式指针,因此您可以编写类似 的语句Myclass object = new Myclass();
,但在 C++ 中这是不可能的。的返回类型new Myclass();
是指向Myclass
对象的指针,即Myclass *
。
回答by Fred Larson
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. This could work in the unlikely event that you have a MyClass constructor that accepts a MyClass pointer.
第二个可能不会编译,因为它试图创建一个 MyClass 对象并将它分配给一个 MyClass 指针。如果您有一个接受 MyClass 指针的 MyClass 构造函数,这可能会起作用。
回答by Deepak Gautam
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();return pointer to class and Myclass object;declares object of class and you are trying to assign pointer to class to the object of class, which is incorrect.
上面的代码不正确,因为new Myclass(); 返回指向类和Myclass 对象的指针;声明类的对象,并且您试图将指向类的指针分配给类的对象,这是不正确的。
Your first code line is correct. But this declares pointer to class not the object of class.
您的第一行代码是正确的。但这声明了指向类的指针而不是类的对象。
Myclass *object = new Myclass(); //declares pointer to class.
To declare object of class you should write following code.
要声明类的对象,您应该编写以下代码。
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. following is code for accessing members of class.
但是需要注意的是,使用类指针和使用类对象访问类成员的方式是不同的。以下是访问类成员的代码。
pointer_to_class->member; // accessing class member using pointer to class
object.member; //accessing class member using object of class
回答by Douglas Leeder
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. And it's probably leaking memory.
第二个一般不会编译。如果它确实编译了,那么该类在构造函数/赋值运算符中做一些复杂的事情。它可能正在泄漏内存。