Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different methods for instantiating an object in C++

Tags:

c++

object

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?

like image 935
RaouL Avatar asked Mar 24 '09 14:03

RaouL


People also ask

What method is used in instantiation of an object?

To instantiate a class in Python, the class like it is called a function, passing the arguments defined by the __init__ method. The newly created object is the return value.

What is instantiating an object?

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor.

Why do we instantiate an object of a class?

Instantiate in Java means to call a constructor of a Class which creates an an instance or object, of the type of that Class. Instantiation allocates the initial memory for the object and returns a reference.

What is instantiation with example?

When you provide a specific example to illustrate an idea, you instantiate it. You say you believe in unicorns, but so far you haven't been able to instantiate that belief.


1 Answers

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

like image 83
Joe Phillips Avatar answered Sep 22 '22 05:09

Joe Phillips