Different methods for instantiating an object in C++

C++Object

C++ Problem Overview


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++ Solutions


Solution 1 - C++

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).

Solution 2 - C++

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. See there for a thorough answer.

Solution 3 - C++

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++.

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:

delete myclass;

Solution 4 - C++

The new operator returns a pointer to the object it creates, so the expression 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();, but in C++ this is simply not possible. The return type of new Myclass(); is a pointer to a Myclass object, i.e. Myclass *.

Solution 5 - C++

The first example creates a pointer to MyClass and initializes it to point to the result of the new operator.

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.

Solution 6 - C++

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.

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 

Solution 7 - C++

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.

Solution 8 - C++

Its posible Myclass name = Myclass();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaouLView Question on Stackoverflow
Solution 1 - C++Joe PhillipsView Answer on Stackoverflow
Solution 2 - C++BenoîtView Answer on Stackoverflow
Solution 3 - C++Vishva RodrigoView Answer on Stackoverflow
Solution 4 - C++WelbogView Answer on Stackoverflow
Solution 5 - C++Fred LarsonView Answer on Stackoverflow
Solution 6 - C++Deepak GautamView Answer on Stackoverflow
Solution 7 - C++Douglas LeederView Answer on Stackoverflow
Solution 8 - C++Fire CodeView Answer on Stackoverflow