This
Item object = new Item();
you will not compile, unless you have declared an Item::Item(Item *item)
constructor. But now is not about that. I think you meant
Item object;
This entry creates an object of the class Item on the stack.
And here
Item *ptObj = new Item();
you declare a pointer-type variable on the stack and put into it the address of the object created on the heap.
The main difference is where the Item
is located. In the first case, it is on the stack. In the second case, on the heap at some address, which is stored in the variable ptObj
, which, in turn, is located on the stack.
Item object = new Item();
are you compiling exactly? (unless of course theItem() {};
andItem(Item*) {};
) constructors are explicitly specified - Vladimir Gamalyan