PDA

View Full Version : C++ making an object without a pointer



NeoDreamer
March 16th, 2009, 03:48 PM
What's the difference between creating an object with a pointer and without a pointer?



MyObject objectName();

MyObject* objectName = new MyObject();


Without a pointer, does the object get destroyed automatically when the function call is over? That is, is it not in dynamic memory and just treated as a primitive variable?

MTsoul
March 16th, 2009, 06:35 PM
It has nothing to do with primitive objects. They can be allocated on the heap too (what you call dynamic memory). If "no pointer is used", then it's allocated on the stack, which means the variable is automatically popped off the stack once the function exits. Basically allocate in heap memory with "new" if you want your variable to stick around after your function exits.