difference between creating an object and instantiating an object
Creating an Object and instantiating an Object is the same thing
Creating an object: with or without `new`
Both do different things.
The first creates an object with automatic storage duration. It is created, used, and then goes out of scope when the current block ({ ... }
) ends. It's the simplest way to create an object, and is just the same as when you write int x = 0;
The second creates an object with dynamic storage duration and allows two things:
Fine control over the lifetime of the object, since it does not go out of scope automatically; you must destroy it explicitly using the keyword
delete
;Creating arrays with a size known only at runtime, since the object creation occurs at runtime. (I won't go into the specifics of allocating dynamic arrays here.)
Neither is preferred; it depends on what you're doing as to which is most appropriate.
Use the former unless you need to use the latter.
Your C++ book should cover this pretty well. If you don't have one, go no further until you have bought and read, several times, one of these.
Good luck.
Your original code is broken, as it delete
s a char
array that it did not new
. In fact, nothing new
d the C-style string; it came from a string literal. delete
ing that is an error (albeit one that will not generate a compilation error, but instead unpredictable behaviour at runtime).
Usually an object should not have the responsibility of delete
ing anything that it didn't itself new
. This behaviour should be well-documented. In this case, the rule is being completely broken.
Difference between creating object with () or without
The other answers correctly state that the parentheses version is actually a function declaration. To understand it intuitively, suppose you wrote MainGUIWindow f();
Looks more like a function, doesn't it? :)
The more interesting question is what is the difference between
MainGUIWindow* p = new MainGUIWindow;
and
MainGUIWindow* p = new MainGUIWindow();
The version with parentheses is called value-initialization, whereas the version without is called default-initialization. For non-POD classes there is no difference between the two. For POD-structs, however, value-initialization involves setting all members to 0,
my2c
Addition: In general, if some syntactic construct can be interpreted both as a declaration and something else, the compiler always resolves the ambiguity in favor of the declaration.
Difference between instantiating a class object with Constructor parameter and * operator without parameters C++
Statement
Shape *shape;
does not create any instance of the class Shape.
It declares a variable that is a pointer of type Shape *
and that is not initialized that is it has indetermined value provided that the declaration declares a local variable.
As for the constructor then the only constructor of the class with two parameters is also the default constructor of the class because each parameter has a default argument.
Shape( int a=0, int b=0)
^^^ ^^^
{
//...
}
Thus you can write for example
Shape sh;
and data members of the created object will be initialized by these default arguments.
This declaration is equivalent to
Shape sh( 0, 0 );
C++ Is there a difference between creating obj with new keyword and creating directly
Yes, it is. The first one creates an object on the stack and return its address. When the function returns the stack unwinds and the object gets destroyed. Therefore the caller ends up with a dangling pointer.
The second allocates an object on the heap and returns the address. The object is valid and will continue to be so until the caller explicitly deletes it.
You should never do the first approach!
What happens if 'new' is used to instantiate an object without assigning it to variable?
new int[5];//without assigning it to a pointer.
Yes, there will be a 5*sizeof(int)
chunk of memory allocated but inaccessible to you, since you didn't save the pointer. You will have a memory leak.
new some_obj_[5];//without assigning it to a pointer.
Yes, there will be 5*sizeof(some_obj_)
chunk of memory allocated but inaccessible to you, since you didn't save the pointer. The default constructor for some_obj_
will be called 5 times. That should be trivial to verify. Depending on how some_obj_
is coded you may have a memory leak.
Related Topics
What Is the Meaning of the Term "Free Function" in C++
What Are the Correct Link Options to Use Std::Thread in Gcc Under Linux
Pre-2016 Valgrind: Memory Still Reachable With Trivial Program Using ≪Iostream≫
C++ Openmp Parallel For Loop - Alternatives to Std::Vector
How to Assume (Bool)True == (Int)1 for Any C++ Compiler
Is Passing Pointer Argument, Pass by Value in C++
Constants and Compiler Optimization in C++
Catch Exception by Pointer in C++
How to Check Whether Operator== Exists
Best C++ Code Formatter/Beautifier
How to Retrieve All Keys (Or Values) from a Std::Map and Put Them into a Vector
Move Assignment Operator and 'If (This != &Rhs)'
How to Initialize Const Member Variable in a Class
How to Explicitly Instantiate a Template Function
I Don't Want My Excel Add-In to Return an Array (Instead I Need a Udf to Change Other Cells)